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