test/TestUtils.cpp
author Jens Alfke <jens@mooseyard.com>
Tue Sep 29 15:46:42 2009 -0700 (2009-09-29)
changeset 9 629f61203db1
parent 8 21a6c17f4e3e
permissions -rw-r--r--
* Merged in jbm's changes for Linux compatibility, fixing Mac compatibility in the process :)
* Fixed two regressions having to do with the _previousTrailerPosition in VersionDictionary.cpp.
* Made sure RTTI is enabled in the OttomanTest target because gtest requires it.
     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 _DARWIN_C_SOURCE
    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                 if (wordLen > 0)
    61                     sWords[sNWords++] = strdup(word); 
    62             }
    63         }
    64     }
    65     
    66 #pragma mark -
    67 #pragma mark TIMER:
    68     
    69     Timer::Timer (const char *operation, int divisor) {
    70         _operation = operation;
    71         _divisor = divisor;
    72         _cpuTime = clock();
    73         _time = now();
    74     }
    75     
    76     Timer::~Timer() {
    77         double elapsedCPU = (clock() - _cpuTime) / 1.0e6;
    78         double elapsed = now() - _time;
    79         printf("### %s took %.6lf sec (used %.6lf sec CPU)", _operation, elapsed, elapsedCPU);
    80         if (_divisor > 1)
    81             printf(" ... per item: %.3lf usec, %.3lf usec CPU", elapsed/_divisor*1e6, elapsedCPU/_divisor*1e6);
    82         printf("\n");
    83     }
    84     
    85     double Timer::now() {
    86         struct timeval t;
    87         gettimeofday(&t,NULL);
    88         return (double)t.tv_sec + t.tv_usec/1.0e6;
    89     }
    90     
    91 }
    92 
    93 
    94 using namespace Mooseyard;
    95 
    96 int main(int argc, char **argv) {
    97 #ifdef _DARWIN_C_SOURCE
    98     srandomdev();
    99 #endif
   100     try {
   101         ::testing::InitGoogleTest(&argc, argv);
   102         return RUN_ALL_TESTS();
   103     } catch (const File::Error &err) {
   104         fprintf(stderr, "\n*** File::Error thrown: %i/%s\n", err.code,err.message);
   105         throw;
   106     }
   107 }