A quick unit testing question!

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
elderK
Member
Member
Posts: 190
Joined: Mon Dec 11, 2006 10:54 am
Location: Dunedin, New Zealand
Contact:

A quick unit testing question!

Post by elderK »

Hey guys, I was wondering if anyone here unit tests their code aggressively? Like, with GCOV and the works?

I've been pondering about how to unit test the bootloader itself, even the bootsector and was curious as to whether anyone else had done something similar.

~K
alexfru
Member
Member
Posts: 1109
Joined: Tue Mar 04, 2014 5:27 am

Re: A quick unit testing question!

Post by alexfru »

gcov doesn't do unit testing or any testing for that matter, nor does it create any tests. It lets you collect code coverage info.

I've written a test suite for my FAT FS code (and run it with gcov to see what's not covered by my test cases).
I've written a test for a memory manager as well (no coverage testing).
I've once tested the hell out of a red-black tree implementation (got 100% or nearly 100% code coverage).
...
For about 6 years I was paid to write tests for low-level stuff... It's an interesting experience.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: A quick unit testing question!

Post by Love4Boobies »

Writing tests only ever makes sense when you believe you've found a bug. If the code is so complicated that it would require aggressive testing, you should probably use a different method to assure quality, such as formal verification.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: A quick unit testing question!

Post by Combuster »

Love4Boobies wrote:Writing tests only ever makes sense when you believe you've found a bug.
Nope. It serves equally well to notice bugs the instance they are created.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: A quick unit testing question!

Post by Love4Boobies »

Combuster wrote:
Love4Boobies wrote:Writing tests only ever makes sense when you believe you've found a bug.
Nope. It serves equally well to notice bugs the instance they are created.
Sure but you can't know whether your code is correct or not in advance. You're left with three choices:
  • Test everything, thus wasting valuable development time.
  • Test arbitrarily. I doubt anyone would agree this is a good method.
  • Only test the code that you suspect is wrong, which is what I suggested.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
sortie
Member
Member
Posts: 930
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: A quick unit testing question!

Post by sortie »

There are methods such as test-driven development where you explicitly never write code unless you have tests that confirms its existence and correctness. This way you quickly grow a large amount of tests and you may even gain more correctness through the simple virtue of writing your code such that it can be tested. However, I get the feeling that unit tests and bootloaders aren't perfect matches. Bootloaders are usually simple enough in principle, but they have to deal and be robust against BIOS bugs and older hardware platforms and it's not trivial to test such cases.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: A quick unit testing question!

Post by Love4Boobies »

I don't think anyone sane would advocate test-driven development. Any experienced developer will tell you that most of the tests they've ever written turned out to be useless. TDD will only make this worse. Besides, a lot of the time, you don't even know what problem you're solving until you've started working on a solution, in which case TDD can result in a premature specification or in discarded tests. The justification I most often hear is that TDD is useful because the tests will ensure everything still holds together whenever there is an architecture or design change---but why not instead write the tests whenever such situations occur?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: A quick unit testing question!

Post by bluemoon »

I found TDD is good for library / framework development. It's also sometime good for game development, like to ensure a certain random logic generate correct statistic outcome. Other than that, I mostly write unit tests when I'm tired :lol:
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: A quick unit testing question!

Post by Love4Boobies »

I said anyone sane. :) Anyway, correctness and packaging are orthogonal issues and I don't think game development is a particularly special type of development.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: A quick unit testing question!

Post by thepowersgang »

Back on to the original question - I have written unit tests for the more self-contained but tricky parts of my codebase (e.g. libc's printf/scanf code), and module tests for the TCP handling.

Other parts are left to be tested by running code that uses it (e.g. a LARGE part of my loading, graphics, and IPC code is tested by running the GUI).

Agressive unit testing is very hard for core OS services, because they usually either interact with hardware/firmware (which is very hard to simulate), or relies on so many other components in the system, as to make doing unit tests a waste of time. The far better solution is to write programs that ensure that the system behaves as expected, and run these programs on whatever hardware configuration you can find.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: A quick unit testing question!

Post by Love4Boobies »

thepowersgang wrote:Agressive unit testing is very hard for core OS services, because they usually either interact with hardware/firmware (which is very hard to simulate), or relies on so many other components in the system, as to make doing unit tests a waste of time. The far better solution is to write programs that ensure that the system behaves as expected, and run these programs on whatever hardware configuration you can find.
The above can be a little misleading. I don't think he is suggesting integration testing doesn't serve a point.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: A quick unit testing question!

Post by Brendan »

Hi,

I wrote this code:

Code: Select all

    int foo(int a) {
        char b;

        b = getchar();
        if(b != 0xAA) {
            b = b ^ 0x55;
        }
        return a/b;
    }
I stubbed out "getchar()" and unit tested it. My unit tests have 100% code coverage, therefore there are no bugs in this function.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
alexfru
Member
Member
Posts: 1109
Joined: Tue Mar 04, 2014 5:27 am

Re: A quick unit testing question!

Post by alexfru »

Brendan wrote: My unit tests have 100% code coverage, therefore there are no bugs in this function.
That may work for this function, but usually 100% coverage is either meaningless (say, we didn't pay attention to coverage of all paths through our code and only focused on basic block coverage or we failed to test some functionality, even though exercised the relevant code (e.g. didn't distinguish passed from failed)) or unreachable (because complexity doesn't give us a chance to test everything in any practical amount of time, add to that code being poorly written from the testability point of view). Coverage can tell you where you clearly failed to test. It can't guaranty correctness. So, I take it, a smiley was lost when writing that statement of yours. Was it not?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: A quick unit testing question!

Post by Combuster »

Brendan example was obviously meant to point out that as long as a test doesn't feed "U" into the system it doesn't find the crash.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: A quick unit testing question!

Post by Candy »

Also got a great example. I've got a function that takes a char * as input and that does a FFT on it. Tested using a vector of 64 zeroes and it works 100%, with 100% test coverage.

I call it memset.
Post Reply