test/TestUtils.cpp
author jbm@humility
Mon Sep 28 23:39:08 2009 -0700 (2009-09-28)
changeset 8 21a6c17f4e3e
parent 0 31a43d94cc26
child 9 629f61203db1
permissions -rw-r--r--
jbm: it compiles... but bombs in unit tests
     1 /*
     2  *  TestUtils.cpp
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 9/12/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #include "TestUtils.h"
    11 #include "File.h"
    12 #include <time.h>
    13 #include <sys/time.h>
    14 #include <stdio.h>
    15 
    16 namespace Mooseyard {
    17 
    18     std::ostream& operator<< (std::ostream &out, const Blob &blob) {
    19         char str[blob.length+1];
    20         memcpy(str,blob.bytes,blob.length);
    21         str[blob.length] = 0;
    22         out << str;
    23         return out;
    24     }
    25     
    26     void shuffle(int a[], int n, unsigned seed) {
    27         if (seed==0) {
    28 #ifdef BSD
    29             srandomdev();
    30 #endif
    31             seed = random();
    32         }
    33         fprintf(stderr,"shuffle(n=%i, seed=%u)\n", n,seed);
    34         srandom(seed);
    35         for (int i=0; i<n-1; i++) {
    36             int j = i + (random() % (n-i));
    37             int temp = a[i];
    38             a[i] = a[j];
    39             a[j] = temp;
    40         }
    41     }
    42     
    43     char **sWords;
    44     int sNWords;
    45     
    46     void readWords() {
    47         if (!sWords) {
    48             sWords = new char*[250000];
    49             sNWords = 0;
    50             
    51             FILE *in = fopen("/usr/share/dict/words", "r");
    52             char word[4096];
    53             size_t wordLen;
    54             while (NULL != ::fgets(word, 4096, in)) {
    55                 wordLen = ::strlen(word);
    56                 if (word[wordLen-1]=='\n') {
    57 		  word[wordLen-1] = '\0';
    58 		  wordLen--;
    59 		}
    60 
    61                 if (wordLen == 0) continue;
    62 
    63                 sWords[sNWords] = strdup(word); 
    64                 //if( sNWords % 10000 == 0)
    65                 //    printf("'%s' ... ", sWords[sNWords]->string());
    66                 sNWords++;
    67             }
    68         }
    69     }
    70     
    71 #pragma mark -
    72 #pragma mark TIMER:
    73     
    74     Timer::Timer (const char *operation, int divisor) {
    75         _operation = operation;
    76         _divisor = divisor;
    77         _cpuTime = clock();
    78         _time = now();
    79     }
    80     
    81     Timer::~Timer() {
    82         double elapsedCPU = (clock() - _cpuTime) / 1.0e6;
    83         double elapsed = now() - _time;
    84         printf("### %s took %.6lf sec (used %.6lf sec CPU)", _operation, elapsed, elapsedCPU);
    85         if (_divisor > 1)
    86             printf(" ... per item: %.3lf usec, %.3lf usec CPU", elapsed/_divisor*1e6, elapsedCPU/_divisor*1e6);
    87         printf("\n");
    88     }
    89     
    90     double Timer::now() {
    91         struct timeval t;
    92         gettimeofday(&t,NULL);
    93         return (double)t.tv_sec + t.tv_usec/1.0e6;
    94     }
    95     
    96 }
    97 
    98 
    99 using namespace Mooseyard;
   100 
   101 int main(int argc, char **argv) {
   102 #if BSD
   103     srandomdev();
   104 #endif
   105     try {
   106         ::testing::InitGoogleTest(&argc, argv);
   107         return RUN_ALL_TESTS();
   108     } catch (const File::Error &err) {
   109         fprintf(stderr, "\n*** File::Error thrown: %i/%s\n", err.code,err.message);
   110         throw;
   111     }
   112 }