File: strcasecmp.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (66 lines) | stat: -rw-r--r-- 1,889 bytes parent folder | download | duplicates (3)
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
// Aggregation $strcasecmp tests.

t = db.jstests_aggregation_strcasecmp;
t.drop();

t.save( {} );

function cmp( a, b ) {
    return t.aggregate( { $project:{ a:{ $cmp:[ a, b ] } } } ).result[ 0 ].a;
}

function strcasecmp( a, b ) {
    return t.aggregate( { $project:{ a:{ $strcasecmp:[ a, b ] } } } ).result[ 0 ].a;
}

function assertException( args ) {
    assert.commandFailed(t.runCommand('aggregate',
                                      {pipeline: [{$project: {a: {$strcasecmp: args}}}]}));
}

function assertStrcasecmp( expected, a, b ) {
    assert.eq( expected, strcasecmp( a, b ) );
    assert.eq( -expected, strcasecmp( b, a ) );    
}

function assertBoth( expectedStrcasecmp, expectedCmp, a, b ) {
    assertStrcasecmp( expectedStrcasecmp, a, b );
    assert.eq( expectedCmp, cmp( a, b ) );
    assert.eq( -expectedCmp, cmp( b, a ) );
}

// Wrong number of arguments.
assertException( [] );
assertException( [ 'a' ] );
assertException( [ 'a', 'b', 'c' ] );

// Basic tests.
assertBoth( 0, 0, '', '' );
assertBoth( -1, -1, '', 'a' );
assertBoth( 0, -1, 'A', 'a' );
assertBoth( 1, -1, 'Ab', 'a' );
assertBoth( 0, -1, 'Ab', 'aB' );
assertBoth( 1, -1, 'Bb', 'aB' );
assertBoth( -1, -1, 'Bb', 'cB' );
assertBoth( 1, -1, 'aB', 'aa' );
assertBoth( -1, -1, 'aB', 'ac' );

// With non alphabet characters.
assertBoth( 0, -1, 'A$_b1C?', 'a$_b1C?' );
assertBoth( 1, -1, 'ABC01234', 'abc0123' );

// String coercion.
assertStrcasecmp( 0, '1', 1 );
assertStrcasecmp( 0, '1.23', 1.23 );
assertStrcasecmp( 0, '1970-01-01T00:00:00', new Date( 0 ) );
assertStrcasecmp( 0, '1970-01-01t00:00:00', new Date( 0 ) );
assertException( [ 'abc', /abc/ ] );

// Extended characters.
assertBoth( 0, -1, '\u0080D\u20ac', '\u0080d\u20ac' );
assertBoth( 1, 1, 'ó', 'Ó' ); // Not treated as equal currently.

// String from field path.
t.drop();
t.save( { x:'abc' } );
assertBoth( 0, 1, '$x', 'ABC' );