1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/TestUtils.cpp Sun Sep 20 15:14:12 2009 -0700
1.3 @@ -0,0 +1,104 @@
1.4 +/*
1.5 + * TestUtils.cpp
1.6 + * Ottoman
1.7 + *
1.8 + * Created by Jens Alfke on 9/12/09.
1.9 + * Copyright 2009 Jens Alfke. All rights reserved.
1.10 + * BSD-Licensed: See the file "LICENSE.txt" for details.
1.11 + */
1.12 +
1.13 +#include "TestUtils.h"
1.14 +#include "File.h"
1.15 +#include <time.h>
1.16 +#include <sys/time.h>
1.17 +
1.18 +namespace Mooseyard {
1.19 +
1.20 + std::ostream& operator<< (std::ostream &out, const Blob &blob) {
1.21 + char str[blob.length+1];
1.22 + memcpy(str,blob.bytes,blob.length);
1.23 + str[blob.length] = 0;
1.24 + out << str;
1.25 + return out;
1.26 + }
1.27 +
1.28 + void shuffle(int a[], int n, unsigned seed) {
1.29 + if (seed==0) {
1.30 + srandomdev();
1.31 + seed = random();
1.32 + }
1.33 + fprintf(stderr,"shuffle(n=%i, seed=%u)\n", n,seed);
1.34 + srandom(seed);
1.35 + for (int i=0; i<n-1; i++) {
1.36 + int j = i + (random() % (n-i));
1.37 + int temp = a[i];
1.38 + a[i] = a[j];
1.39 + a[j] = temp;
1.40 + }
1.41 + }
1.42 +
1.43 + char **sWords;
1.44 + int sNWords;
1.45 +
1.46 + void readWords() {
1.47 + if (!sWords) {
1.48 + sWords = new char*[250000];
1.49 + sNWords = 0;
1.50 +
1.51 + FILE *in = fopen("/usr/share/dict/words", "r");
1.52 + const char *word;
1.53 + size_t wordLen;
1.54 + while (NULL != (word=fgetln(in,&wordLen))) {
1.55 + if (word[wordLen-1]=='\n')
1.56 + wordLen--;
1.57 + char *str = new char[wordLen+1];
1.58 + memcpy(str, word, wordLen);
1.59 + str[wordLen] = 0;
1.60 + sWords[sNWords] = str;
1.61 + //if( sNWords % 10000 == 0)
1.62 + // printf("'%s' ... ", sWords[sNWords]->string());
1.63 + sNWords++;
1.64 + }
1.65 + }
1.66 + }
1.67 +
1.68 +#pragma mark -
1.69 +#pragma mark TIMER:
1.70 +
1.71 + Timer::Timer (const char *operation, int divisor) {
1.72 + _operation = operation;
1.73 + _divisor = divisor;
1.74 + _cpuTime = clock();
1.75 + _time = now();
1.76 + }
1.77 +
1.78 + Timer::~Timer() {
1.79 + double elapsedCPU = (clock() - _cpuTime) / 1.0e6;
1.80 + double elapsed = now() - _time;
1.81 + printf("### %s took %.6lf sec (used %.6lf sec CPU)", _operation, elapsed, elapsedCPU);
1.82 + if (_divisor > 1)
1.83 + printf(" ... per item: %.3lf usec, %.3lf usec CPU", elapsed/_divisor*1e6, elapsedCPU/_divisor*1e6);
1.84 + printf("\n");
1.85 + }
1.86 +
1.87 + double Timer::now() {
1.88 + struct timeval t;
1.89 + gettimeofday(&t,NULL);
1.90 + return (double)t.tv_sec + t.tv_usec/1.0e6;
1.91 + }
1.92 +
1.93 +}
1.94 +
1.95 +
1.96 +using namespace Mooseyard;
1.97 +
1.98 +int main(int argc, char **argv) {
1.99 + srandomdev();
1.100 + try {
1.101 + ::testing::InitGoogleTest(&argc, argv);
1.102 + return RUN_ALL_TESTS();
1.103 + } catch (const File::Error &err) {
1.104 + fprintf(stderr, "\n*** File::Error thrown: %i/%s\n", err.code,err.message);
1.105 + throw;
1.106 + }
1.107 +}