Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    /*
    ** Test procedure for ParseRfc822Date
    */
    void TestParseRfc822Date(void){
      time_t t1, t2;
      for(t1=0; t1<0x7fffffff; t1 += 127){
        t2 = ParseRfc822Date(Rfc822Date(t1));
        assert( t1==t2 );
      }
    }
There's only two billion integers, guess we can test them all. Well, substantially fewer than two billion with that skip. I wonder if that completes in a few seconds.


That was quite easy to cut and paste and compile:

    $ time ./althttpd-time-parse 
    Test completed in 10518961 us

    real    0m10,521s
    user    0m10,486s
    sys     0m0,004s
The "Test completed" line is from my main() "driver", I wanted to measure time inline too and the measurements seem to agree.

This is on a Dell Latitude featuring a Core i5-7300U at 2.6 GHz, running Ubuntu 20.10.


This loop doesn’t test them all - it tests every 1/127th integer, so it only runs about 16 million times.


And a modern CPU runs billions of instructions per second, so it shouldn't take too long at all. Depends on the implementation of the function as well though, and how the compiler can optimize, unroll or optimize both the function under test and the test itself.



How would you write a better test?

IMO most tests are fundamentally flawed. The way most testing is done it would be easier and better to just write everything twice, have a method to compare results, and hope you got it right at least once.


I do think of tests as a kind of double accounting.


Computers are insanely fast


Another comment shows 10 seconds on a relatively decent CPU from 2017. So it is a fairly heavyweight task, though I suppose could be rewritten to use more than one core.


yeah, that's a lot of iterations :-)

I was curious to see how my M1 compares to my intel 2019 macbook pro:

M1:

/tmp/tt 8.42s user 0.01s system 99% cpu 8.435 total

2,6 GHz 6-Core Intel Core i7

/tmp/tt 15.69s user 0.03s system 98% cpu 15.888 total

/tmp/tt 15.69s user 0.03s system 98% cpu 15.888 total


Looks like test code, rather than something that runs when serving in production.

It fails an assert if the parse doesn't work I guess?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: