test/TestUtils.cpp
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 15:14:12 2009 -0700 (2009-09-20)
changeset 0 31a43d94cc26
child 8 21a6c17f4e3e
permissions -rw-r--r--
First official checkin.
     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 
    15 namespace Mooseyard {
    16 
    17     std::ostream& operator<< (std::ostream &out, const Blob &blob) {
    18         char str[blob.length+1];
    19         memcpy(str,blob.bytes,blob.length);
    20         str[blob.length] = 0;
    21         out << str;
    22         return out;
    23     }
    24     
    25     void shuffle(int a[], int n, unsigned seed) {
    26         if (seed==0) {
    27             srandomdev();
    28             seed = random();
    29         }
    30         fprintf(stderr,"shuffle(n=%i, seed=%u)\n", n,seed);
    31         srandom(seed);
    32         for (int i=0; i<n-1; i++) {
    33             int j = i + (random() % (n-i));
    34             int temp = a[i];
    35             a[i] = a[j];
    36             a[j] = temp;
    37         }
    38     }
    39     
    40     char **sWords;
    41     int sNWords;
    42     
    43     void readWords() {
    44         if (!sWords) {
    45             sWords = new char*[250000];
    46             sNWords = 0;
    47             
    48             FILE *in = fopen("/usr/share/dict/words", "r");
    49             const char *word;
    50             size_t wordLen;
    51             while (NULL != (word=fgetln(in,&wordLen))) {
    52                 if (word[wordLen-1]=='\n')
    53                     wordLen--;
    54                 char *str = new char[wordLen+1];
    55                 memcpy(str, word, wordLen);
    56                 str[wordLen] = 0;
    57                 sWords[sNWords] = str;
    58                 //if( sNWords % 10000 == 0)
    59                 //    printf("'%s' ... ", sWords[sNWords]->string());
    60                 sNWords++;
    61             }
    62         }
    63     }
    64     
    65 #pragma mark -
    66 #pragma mark TIMER:
    67     
    68     Timer::Timer (const char *operation, int divisor) {
    69         _operation = operation;
    70         _divisor = divisor;
    71         _cpuTime = clock();
    72         _time = now();
    73     }
    74     
    75     Timer::~Timer() {
    76         double elapsedCPU = (clock() - _cpuTime) / 1.0e6;
    77         double elapsed = now() - _time;
    78         printf("### %s took %.6lf sec (used %.6lf sec CPU)", _operation, elapsed, elapsedCPU);
    79         if (_divisor > 1)
    80             printf(" ... per item: %.3lf usec, %.3lf usec CPU", elapsed/_divisor*1e6, elapsedCPU/_divisor*1e6);
    81         printf("\n");
    82     }
    83     
    84     double Timer::now() {
    85         struct timeval t;
    86         gettimeofday(&t,NULL);
    87         return (double)t.tv_sec + t.tv_usec/1.0e6;
    88     }
    89     
    90 }
    91 
    92 
    93 using namespace Mooseyard;
    94 
    95 int main(int argc, char **argv) {
    96     srandomdev();
    97     try {
    98         ::testing::InitGoogleTest(&argc, argv);
    99         return RUN_ALL_TESTS();
   100     } catch (const File::Error &err) {
   101         fprintf(stderr, "\n*** File::Error thrown: %i/%s\n", err.code,err.message);
   102         throw;
   103     }
   104 }