summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/basictests.cpp
blob: a83020d76ebe66f4164f181c557fa57cf4e13a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// basictests.cpp : basic unit tests
//

/**
 *    Copyright (C) 2009 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "pch.h"

#include "dbtests.h"
#include "../util/base64.h"
#include "../util/array.h"
#include "../util/text.h"
#include "../util/queue.h"
#include "../util/paths.h"
#include "../util/stringutils.h"
#include "../util/compress.h"
#include "../util/time_support.h"
#include "../db/db.h"

namespace BasicTests {

    class Rarely {
    public:
        void run() {
            int first = 0;
            int second = 0;
            int third = 0;
            for( int i = 0; i < 128; ++i ) {
                incRarely( first );
                incRarely2( second );
                ONCE ++third;
            }
            ASSERT_EQUALS( 1, first );
            ASSERT_EQUALS( 1, second );
            ASSERT_EQUALS( 1, third );
        }
    private:
        void incRarely( int &c ) {
            RARELY ++c;
        }
        void incRarely2( int &c ) {
            RARELY ++c;
        }
    };

    class Base64Tests {
    public:

        void roundTrip( string s ) {
            ASSERT_EQUALS( s , base64::decode( base64::encode( s ) ) );
        }

        void roundTrip( const unsigned char * _data , int len ) {
            const char *data = (const char *) _data;
            string s = base64::encode( data , len );
            string out = base64::decode( s );
            ASSERT_EQUALS( out.size() , static_cast<size_t>(len) );
            bool broke = false;
            for ( int i=0; i<len; i++ ) {
                if ( data[i] != out[i] )
                    broke = true;
            }
            if ( ! broke )
                return;

            cout << s << endl;
            for ( int i=0; i<len; i++ )
                cout << hex << ( data[i] & 0xFF ) << dec << " ";
            cout << endl;
            for ( int i=0; i<len; i++ )
                cout << hex << ( out[i] & 0xFF ) << dec << " ";
            cout << endl;

            ASSERT(0);
        }

        void run() {

            ASSERT_EQUALS( "ZWxp" , base64::encode( "eli" , 3 ) );
            ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" , 6 ) );
            ASSERT_EQUALS( "ZWxpb3Rz" , base64::encode( "eliots" ) );

            ASSERT_EQUALS( "ZQ==" , base64::encode( "e" , 1 ) );
            ASSERT_EQUALS( "ZWw=" , base64::encode( "el" , 2 ) );

            roundTrip( "e" );
            roundTrip( "el" );
            roundTrip( "eli" );
            roundTrip( "elio" );
            roundTrip( "eliot" );
            roundTrip( "eliots" );
            roundTrip( "eliotsz" );

            unsigned char z[] = { 0x1 , 0x2 , 0x3 , 0x4 };
            roundTrip( z , 4 );

            unsigned char y[] = {
                0x01, 0x10, 0x83, 0x10, 0x51, 0x87, 0x20, 0x92, 0x8B, 0x30,
                0xD3, 0x8F, 0x41, 0x14, 0x93, 0x51, 0x55, 0x97, 0x61, 0x96,
                0x9B, 0x71, 0xD7, 0x9F, 0x82, 0x18, 0xA3, 0x92, 0x59, 0xA7,
                0xA2, 0x9A, 0xAB, 0xB2, 0xDB, 0xAF, 0xC3, 0x1C, 0xB3, 0xD3,
                0x5D, 0xB7, 0xE3, 0x9E, 0xBB, 0xF3, 0xDF, 0xBF
            };
            roundTrip( y , 4 );
            roundTrip( y , 40 );
        }
    };

    namespace stringbuildertests {
#define SBTGB(x) ss << (x); sb << (x);

        class Base {
            virtual void pop() = 0;

        public:
            Base() {}
            virtual ~Base() {}

            void run() {
                pop();
                ASSERT_EQUALS( ss.str() , sb.str() );
            }

            stringstream ss;
            StringBuilder sb;
        };

        class simple1 : public Base {
            void pop() {
                SBTGB(1);
                SBTGB("yo");
                SBTGB(2);
            }
        };

        class simple2 : public Base {
            void pop() {
                SBTGB(1);
                SBTGB("yo");
                SBTGB(2);
                SBTGB( 12123123123LL );
                SBTGB( "xxx" );
                SBTGB( 5.4 );
                SBTGB( 5.4312 );
                SBTGB( "yyy" );
                SBTGB( (short)5 );
                SBTGB( (short)(1231231231231LL) );
            }
        };

        class reset1 {
        public:
            void run() {
                StringBuilder sb;
                sb << "1" << "abc" << "5.17";
                ASSERT_EQUALS( "1abc5.17" , sb.str() );
                ASSERT_EQUALS( "1abc5.17" , sb.str() );
                sb.reset();
                ASSERT_EQUALS( "" , sb.str() );
                sb << "999";
                ASSERT_EQUALS( "999" , sb.str() );
            }
        };

        class reset2 {
        public:
            void run() {
                StringBuilder sb;
                sb << "1" << "abc" << "5.17";
                ASSERT_EQUALS( "1abc5.17" , sb.str() );
                ASSERT_EQUALS( "1abc5.17" , sb.str() );
                sb.reset(1);
                ASSERT_EQUALS( "" , sb.str() );
                sb << "999";
                ASSERT_EQUALS( "999" , sb.str() );
            }
        };

    }

    class sleeptest {
    public:

        void run() {
            Timer t;
            int matches = 0;
            for( int p = 0; p < 3; p++ ) {
                sleepsecs( 1 );
                int sec = (t.millis() + 2)/1000;
                if( sec == 1 ) 
                    matches++;
                else
                    log() << "temp millis: " << t.millis() << endl;
                ASSERT( sec >= 0 && sec <= 2 );
                t.reset();
            }
            if ( matches < 2 )
                log() << "matches:" << matches << endl;
            ASSERT( matches >= 2 );

            sleepmicros( 1527123 );
            ASSERT( t.micros() > 1000000 );
            ASSERT( t.micros() < 2000000 );

            t.reset();
            sleepmillis( 1727 );
            ASSERT( t.millis() >= 1000 );
            ASSERT( t.millis() <= 2500 );

            {
                int total = 1200;
                int ms = 2;
                t.reset();
                for ( int i=0; i<(total/ms); i++ ) {
                    sleepmillis( ms );
                }
                {
                    int x = t.millis();
                    if ( x < 1000 || x > 2500 ) {
                        cout << "sleeptest finds sleep accuracy to be not great. x: " << x << endl;
                        ASSERT( x >= 1000 );
                        ASSERT( x <= 20000 );
                    }
                }
            }

#ifdef __linux__
            {
                int total = 1200;
                int micros = 100;
                t.reset();
                int numSleeps = 1000*(total/micros);
                for ( int i=0; i<numSleeps; i++ ) {
                    sleepmicros( micros );
                }
                {
                    int y = t.millis();
                    if ( y < 1000 || y > 2500 ) {
                        cout << "sleeptest y: " << y << endl;
                        ASSERT( y >= 1000 );
                        /* ASSERT( y <= 100000 ); */
                    }
                }
            }
#endif

        }

    };

    class SleepBackoffTest {
    public:
        void run() {

            int maxSleepTimeMillis = 1000;
            int lastSleepTimeMillis = -1;
            int epsMillis = 50; // Allowable inprecision for timing

            Backoff backoff( maxSleepTimeMillis, maxSleepTimeMillis * 2 );

            Timer t;

            // Make sure our backoff increases to the maximum value
            int maxSleepCount = 0;
            while( maxSleepCount < 3 ){

                t.reset();

                backoff.nextSleepMillis();

                int elapsedMillis = t.millis();

                log() << "Slept for " << elapsedMillis << endl;

                ASSERT( almostGTE( elapsedMillis, lastSleepTimeMillis, epsMillis ) );
                lastSleepTimeMillis = elapsedMillis;

                if( almostEq( elapsedMillis, maxSleepTimeMillis, epsMillis ) ) maxSleepCount++;
            }

            // Make sure that our backoff gets reset if we wait much longer than the maximum wait
            sleepmillis( maxSleepTimeMillis * 4 );

            t.reset();
            backoff.nextSleepMillis();

            ASSERT( almostEq( t.millis(), 0, epsMillis ) );

        }

        bool almostEq( int a, int b, int eps ){
            return std::abs( a - b ) <= eps;
        }

        bool almostGTE( int a, int b, int eps ){
            if( almostEq( a, b, eps ) ) return true;
            return a > b;
        }
    };

    class AssertTests {
    public:

        int x;

        AssertTests() {
            x = 0;
        }

        string foo() {
            x++;
            return "";
        }
        void run() {
            uassert( -1 , foo() , 1 );
            if( x != 0 ) {
                ASSERT_EQUALS( 0 , x );
            }
            try {
                uassert( -1 , foo() , 0 );
            }
            catch ( ... ) {}
            ASSERT_EQUALS( 1 , x );
        }
    };

    namespace ArrayTests {
        class basic1 {
        public:
            void run() {
                FastArray<int> a(100);
                a.push_back( 5 );
                a.push_back( 6 );

                ASSERT_EQUALS( 2 , a.size() );

                FastArray<int>::iterator i = a.begin();
                ASSERT( i != a.end() );
                ASSERT_EQUALS( 5 , *i );
                ++i;
                ASSERT( i != a.end() );
                ASSERT_EQUALS( 6 , *i );
                ++i;
                ASSERT( i == a.end() );
            }
        };
    };

    class ThreadSafeStringTest {
    public:
        void run() {
            ThreadSafeString s;
            s = "eliot";
            ASSERT_EQUALS( s , "eliot" );
            ASSERT( s != "eliot2" );

            ThreadSafeString s2 = s;
            ASSERT_EQUALS( s2 , "eliot" );


            {
                string foo;
                {
                    ThreadSafeString bar;
                    bar = "eliot2";
                    foo = bar.toString();
                }
                ASSERT_EQUALS( "eliot2" , foo );
            }
        }
    };

    class LexNumCmp {
    public:
        static void assertCmp( int expected, const char *s1, const char *s2,
                              bool lexOnly = false ) {
            mongo::LexNumCmp cmp( lexOnly );
            ASSERT_EQUALS( expected, cmp.cmp( s1, s2, lexOnly ) );
            ASSERT_EQUALS( expected, cmp.cmp( s1, s2 ) );
            ASSERT_EQUALS( expected < 0, cmp( s1, s2 ) );
            ASSERT_EQUALS( expected < 0, cmp( string( s1 ), string( s2 ) ) );
        }
        void run() {

            ASSERT( ! isNumber( (char)255 ) );

            assertCmp( 0, "a", "a" );
            assertCmp( -1, "a", "aa" );
            assertCmp( 1, "aa", "a" );
            assertCmp( -1, "a", "b" );
            assertCmp( 1, "100", "50" );
            assertCmp( -1, "50", "100" );
            assertCmp( 1, "b", "a" );
            assertCmp( 0, "aa", "aa" );
            assertCmp( -1, "aa", "ab" );
            assertCmp( 1, "ab", "aa" );
            assertCmp( 1, "0", "a" );
            assertCmp( 1, "a0", "aa" );
            assertCmp( -1, "a", "0" );
            assertCmp( -1, "aa", "a0" );
            assertCmp( 0, "0", "0" );
            assertCmp( 0, "10", "10" );
            assertCmp( -1, "1", "10" );
            assertCmp( 1, "10", "1" );
            assertCmp( 1, "11", "10" );
            assertCmp( -1, "10", "11" );
            assertCmp( 1, "f11f", "f10f" );
            assertCmp( -1, "f10f", "f11f" );
            assertCmp( -1, "f11f", "f111" );
            assertCmp( 1, "f111", "f11f" );
            assertCmp( -1, "f12f", "f12g" );
            assertCmp( 1, "f12g", "f12f" );
            assertCmp( 1, "aa{", "aab" );
            assertCmp( -1, "aa{", "aa1" );
            assertCmp( -1, "a1{", "a11" );
            assertCmp( 1, "a1{a", "a1{" );
            assertCmp( -1, "a1{", "a1{a" );
            assertCmp( 1, "21", "11" );
            assertCmp( -1, "11", "21" );

            assertCmp( -1 , "a.0" , "a.1" );
            assertCmp( -1 , "a.0.b" , "a.1" );

            assertCmp( -1 , "b." , "b.|" );
            assertCmp( -1 , "b.0e" , (string("b.") + (char)255).c_str() );
            assertCmp( -1 , "b." , "b.0e" );

            assertCmp( 0, "238947219478347782934718234", "238947219478347782934718234");
            assertCmp( 0, "000238947219478347782934718234", "238947219478347782934718234");
            assertCmp( 1, "000238947219478347782934718235", "238947219478347782934718234");
            assertCmp( -1, "238947219478347782934718234", "238947219478347782934718234.1");
            assertCmp( 0, "238", "000238");
            assertCmp( 0, "002384", "0002384");
            assertCmp( 0, "00002384", "0002384");
            assertCmp( 0, "0", "0");
            assertCmp( 0, "0000", "0");
            assertCmp( 0, "0", "000");
            assertCmp( -1, "0000", "0.0");
            assertCmp( 1, "2380", "238");
            assertCmp( 1, "2385", "2384");
            assertCmp( 1, "2385", "02384");
            assertCmp( 1, "2385", "002384");
            assertCmp( -1, "123.234.4567", "00238");
            assertCmp( 0, "123.234", "00123.234");
            assertCmp( 0, "a.123.b", "a.00123.b");
            assertCmp( 1, "a.123.b", "a.b.00123.b");
            assertCmp( -1, "a.00.0", "a.0.1");
            assertCmp( 0, "01.003.02", "1.3.2");
            assertCmp( -1, "1.3.2", "10.300.20");
            assertCmp( 0, "10.300.20", "000000000000010.0000300.000000020");
            assertCmp( 0, "0000a", "0a");
            assertCmp( -1, "a", "0a");
            assertCmp( -1, "000a", "001a");
            assertCmp( 0, "010a", "0010a");
            
            assertCmp( -1 , "a0" , "a00" );
            assertCmp( 0 , "a.0" , "a.00" );
            assertCmp( -1 , "a.b.c.d0" , "a.b.c.d00" );
            assertCmp( 1 , "a.b.c.0.y" , "a.b.c.00.x" );
            
            assertCmp( -1, "a", "a-" );
            assertCmp( 1, "a-", "a" );
            assertCmp( 0, "a-", "a-" );

            assertCmp( -1, "a", "a-c" );
            assertCmp( 1, "a-c", "a" );
            assertCmp( 0, "a-c", "a-c" );

            assertCmp( 1, "a-c.t", "a.t" );
            assertCmp( -1, "a.t", "a-c.t" );
            assertCmp( 0, "a-c.t", "a-c.t" );

            assertCmp( 1, "ac.t", "a.t" );
            assertCmp( -1, "a.t", "ac.t" );
            assertCmp( 0, "ac.t", "ac.t" );            
        }
    };
    
    class LexNumCmpLexOnly : public LexNumCmp {
    public:
        void run() {
            assertCmp( -1, "0", "00", true );
            assertCmp( 1, "1", "01", true );
            assertCmp( -1, "1", "11", true );
            assertCmp( 1, "2", "11", true );
        }
    };

    class DatabaseOwnsNS {
    public:
        void run() {
            Lock::GlobalWrite lk;
            bool isNew = false;
            // this leaks as ~Database is private
            // if that changes, should put this on the stack
            {
                Database * db = new Database( "dbtests_basictests_ownsns" , isNew );
                verify( isNew );

                ASSERT( db->ownsNS( "dbtests_basictests_ownsns.x" ) );
                ASSERT( db->ownsNS( "dbtests_basictests_ownsns.x.y" ) );
                ASSERT( ! db->ownsNS( "dbtests_basictests_ownsn.x.y" ) );
                ASSERT( ! db->ownsNS( "dbtests_basictests_ownsnsa.x.y" ) );
            }
        }
    };

    class NSValidNames {
    public:
        void run() {
            ASSERT( isValidNS( "test.foo" ) );
            ASSERT( ! isValidNS( "test." ) );
            ASSERT( ! isValidNS( "test" ) );
        }
    };

    class PtrTests {
    public:
        void run() {
            scoped_ptr<int> p1 (new int(1));
            boost::shared_ptr<int> p2 (new int(2));
            scoped_ptr<const int> p3 (new int(3));
            boost::shared_ptr<const int> p4 (new int(4));

            //non-const
            ASSERT_EQUALS( p1.get() , ptr<int>(p1) );
            ASSERT_EQUALS( p2.get() , ptr<int>(p2) );
            ASSERT_EQUALS( p2.get() , ptr<int>(p2.get()) ); // T* constructor
            ASSERT_EQUALS( p2.get() , ptr<int>(ptr<int>(p2)) ); // copy constructor
            ASSERT_EQUALS( *p2      , *ptr<int>(p2));
            ASSERT_EQUALS( p2.get() , ptr<boost::shared_ptr<int> >(&p2)->get() ); // operator->

            //const
            ASSERT_EQUALS( p1.get() , ptr<const int>(p1) );
            ASSERT_EQUALS( p2.get() , ptr<const int>(p2) );
            ASSERT_EQUALS( p2.get() , ptr<const int>(p2.get()) );
            ASSERT_EQUALS( p3.get() , ptr<const int>(p3) );
            ASSERT_EQUALS( p4.get() , ptr<const int>(p4) );
            ASSERT_EQUALS( p4.get() , ptr<const int>(p4.get()) );
            ASSERT_EQUALS( p2.get() , ptr<const int>(ptr<const int>(p2)) );
            ASSERT_EQUALS( p2.get() , ptr<const int>(ptr<int>(p2)) ); // constizing copy constructor
            ASSERT_EQUALS( *p2      , *ptr<int>(p2));
            ASSERT_EQUALS( p2.get() , ptr<const boost::shared_ptr<int> >(&p2)->get() );

            //bool context
            ASSERT( ptr<int>(p1) );
            ASSERT( !ptr<int>(NULL) );
            ASSERT( !ptr<int>() );

#if 0
            // These shouldn't compile
            ASSERT_EQUALS( p3.get() , ptr<int>(p3) );
            ASSERT_EQUALS( p4.get() , ptr<int>(p4) );
            ASSERT_EQUALS( p2.get() , ptr<int>(ptr<const int>(p2)) );
#endif
        }
    };

    struct StringSplitterTest {

        void test( string s ) {
            vector<string> v = StringSplitter::split( s , "," );
            ASSERT_EQUALS( s , StringSplitter::join( v , "," ) );
        }

        void run() {
            test( "a" );
            test( "a,b" );
            test( "a,b,c" );

            vector<string> x = StringSplitter::split( "axbxc" , "x" );
            ASSERT_EQUALS( 3 , (int)x.size() );
            ASSERT_EQUALS( "a" , x[0] );
            ASSERT_EQUALS( "b" , x[1] );
            ASSERT_EQUALS( "c" , x[2] );

            x = StringSplitter::split( "axxbxxc" , "xx" );
            ASSERT_EQUALS( 3 , (int)x.size() );
            ASSERT_EQUALS( "a" , x[0] );
            ASSERT_EQUALS( "b" , x[1] );
            ASSERT_EQUALS( "c" , x[2] );

        }
    };

    struct IsValidUTF8Test {
// macros used to get valid line numbers
#define good(s)  ASSERT(isValidUTF8(s));
#define bad(s)   ASSERT(!isValidUTF8(s));

        void run() {
            good("A");
            good("\xC2\xA2"); // cent: ¢
            good("\xE2\x82\xAC"); // euro: €
            good("\xF0\x9D\x90\x80"); // Blackboard A: 𝐀

            //abrupt end
            bad("\xC2");
            bad("\xE2\x82");
            bad("\xF0\x9D\x90");
            bad("\xC2 ");
            bad("\xE2\x82 ");
            bad("\xF0\x9D\x90 ");

            //too long
            bad("\xF8\x80\x80\x80\x80");
            bad("\xFC\x80\x80\x80\x80\x80");
            bad("\xFE\x80\x80\x80\x80\x80\x80");
            bad("\xFF\x80\x80\x80\x80\x80\x80\x80");

            bad("\xF5\x80\x80\x80"); // U+140000 > U+10FFFF
            bad("\x80"); //cant start with continuation byte
            bad("\xC0\x80"); // 2-byte version of ASCII NUL
#undef good
#undef bad
        }
    };


    class QueueTest {
    public:
        void run() {
            BlockingQueue<int> q;
            Timer t;
            int x;
            ASSERT( ! q.blockingPop( x , 5 ) );
            ASSERT( t.seconds() > 3 && t.seconds() < 9 );

        }
    };

    class StrTests {
    public:

        void run() {
            ASSERT_EQUALS( 1u , str::count( "abc" , 'b' ) );
            ASSERT_EQUALS( 3u , str::count( "babab" , 'b' ) );
        }

    };

    class HostAndPortTests {
    public:
        void run() {
            HostAndPort a( "x1" , 1000 );
            HostAndPort b( "x1" , 1000 );
            HostAndPort c( "x1" , 1001 );
            HostAndPort d( "x2" , 1000 );

            ASSERT( a == b );
            ASSERT( a != c );
            ASSERT( a != d );

        }
    };

    class RelativePathTest {
    public:
        void run() {
            RelativePath a = RelativePath::fromRelativePath( "a" );
            RelativePath b = RelativePath::fromRelativePath( "a" );
            RelativePath c = RelativePath::fromRelativePath( "b" );
            RelativePath d = RelativePath::fromRelativePath( "a/b" );


            ASSERT( a == b );
            ASSERT( a != c );
            ASSERT( a != d );
            ASSERT( c != d );
        }
    };

    class CmdLineParseConfigTest {
    public:
        void run() {
            stringstream ss1;
            istringstream iss1("");
            CmdLine::parseConfigFile( iss1, ss1 );
            stringstream ss2;
            istringstream iss2("password=\'foo bar baz\'");
            CmdLine::parseConfigFile( iss2, ss2 );
            stringstream ss3;
            istringstream iss3("\t    this = false  \n#that = true\n  #another = whocares\n\n  other = monkeys  ");
            CmdLine::parseConfigFile( iss3, ss3 );

            ASSERT( ss1.str().compare("\n") == 0 );
            ASSERT( ss2.str().compare("password=\'foo bar baz\'\n\n") == 0 );
            ASSERT( ss3.str().compare("\n  other = monkeys  \n\n") == 0 );
        }
    };

    struct CompressionTest1 { 
        void run() { 
            const char * c = "this is a test";
            std::string s;
            size_t len = compress(c, strlen(c)+1, &s);
            verify( len > 0 );
            
            std::string out;
            bool ok = uncompress(s.c_str(), s.size(), &out);
            verify(ok);
            verify( strcmp(out.c_str(), c) == 0 );
        }
    } ctest1;

    /** Simple tests for log tees. */
    class LogTee {
    public:
        ~LogTee() {
            // Clean global tees on test failure.
            Logstream::get().removeGlobalTee( &_tee );            
        }
        void run() {
            // Attempting to remove a tee before any tees are added is safe.
            Logstream::get().removeGlobalTee( &_tee );

            // A log is not written to a non global tee.
            log() << "LogTee test" << endl;
            assertNumLogs( 0 );

            // A log is written to a global tee.
            Logstream::get().addGlobalTee( &_tee );
            log() << "LogTee test" << endl;
            assertNumLogs( 1 );

            // A log is not written to a tee removed from the global tee list.
            Logstream::get().removeGlobalTee( &_tee );
            log() << "LogTee test" << endl;
            assertNumLogs( 1 );            
        }
    private:
        void assertNumLogs( int expected ) const {
            ASSERT_EQUALS( expected, _tee.numLogs() );
        }
        class Tee : public mongo::Tee {
        public:
            Tee() :
                _numLogs() {
            }
            virtual void write( LogLevel level, const string &str ) {
                ++_numLogs;
            }
            int numLogs() const { return _numLogs; }
        private:
            int _numLogs;
        };
        Tee _tee;
    };

    class All : public Suite {
    public:
        All() : Suite( "basic" ) {
        }

        void setupTests() {
            add< Rarely >();
            add< Base64Tests >();

            add< stringbuildertests::simple1 >();
            add< stringbuildertests::simple2 >();
            add< stringbuildertests::reset1 >();
            add< stringbuildertests::reset2 >();

            add< sleeptest >();
            add< SleepBackoffTest >();
            add< AssertTests >();

            add< ArrayTests::basic1 >();
            add< LexNumCmp >();
            add< LexNumCmpLexOnly >();

            add< DatabaseOwnsNS >();

            add< NSValidNames >();

            add< PtrTests >();

            add< StringSplitterTest >();
            add< IsValidUTF8Test >();

            add< QueueTest >();

            add< StrTests >();

            add< HostAndPortTests >();
            add< RelativePathTest >();
            add< CmdLineParseConfigTest >();

            add< CompressionTest1 >();

            add< LogTee >();
        }
    } myall;

} // namespace BasicTests