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