summaryrefslogtreecommitdiff
path: root/jstests/capped2.js
blob: 65bb82f4c077b05cbf6266b94bbffb6d7b255ee5 (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
db.capped2.drop();
db._dbCommand( { create: "capped2", capped: true, size: 1000, $nExtents: 11, autoIndexId: false } );
tzz = db.capped2;

function debug( x ) {
//    print( x );
}

var val = new Array( 2000 );
var c = "";
for( i = 0; i < 2000; ++i, c += "---" ) { // bigger and bigger objects through the array...
    val[ i ] = { a: c };
}

function checkIncreasing( i ) {
    res = tzz.find().sort( { $natural: -1 } );
    assert( res.hasNext(), "A" );
    var j = i;
    while( res.hasNext() ) {
        try {
             assert.eq( val[ j-- ].a, res.next().a, "B" );
        } catch( e ) {
            debug( "capped2 err " + j );
            throw e;
        }
    }
    res = tzz.find().sort( { $natural: 1 } );
    assert( res.hasNext(), "C" );
    while( res.hasNext() )
	assert.eq( val[ ++j ].a, res.next().a, "D" );
    assert.eq( j, i, "E" );
}

function checkDecreasing( i ) {
    res = tzz.find().sort( { $natural: -1 } );
    assert( res.hasNext(), "F" );
    var j = i;
    while( res.hasNext() ) {
      	assert.eq( val[ j++ ].a, res.next().a, "G" );
    }
    res = tzz.find().sort( { $natural: 1 } );
    assert( res.hasNext(), "H" );
    while( res.hasNext() )
	assert.eq( val[ --j ].a, res.next().a, "I" );
    assert.eq( j, i, "J" );
}

for( i = 0 ;; ++i ) {
    debug( "capped 2: " + i );
    tzz.insert( val[ i ] );
    if ( tzz.count() == 0 ) {
    	assert( i > 100, "K" );
        break;
    }
    checkIncreasing( i );
}

for( i = 600 ; i >= 0 ; --i ) {
    debug( "capped 2: " + i );
    tzz.insert( val[ i ] );
    checkDecreasing( i );
}