summaryrefslogtreecommitdiff
path: root/chromium/third_party/trace-viewer/src/base/iteration_helpers_test.js
blob: 146030cec0b2664c4a1ec1c3193f26dbe565c265 (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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

'use strict';

base.require('base.iteration_helpers');

base.unittest.testSuite('base.iteration_helpers', function() {
  var comparePossiblyUndefinedValues = base.comparePossiblyUndefinedValues;
  var compareArrays = base.compareArrays;

  test('comparePossiblyUndefinedValues', function() {
    function cmp(x, y) {
      assertNotUndefined(x);
      assertNotUndefined(y);
      return x - y;
    }

    assertTrue(comparePossiblyUndefinedValues(0, 1, cmp) < 0);
    assertTrue(comparePossiblyUndefinedValues(1, 0, cmp) > 0);
    assertTrue(comparePossiblyUndefinedValues(1, 1, cmp) == 0);

    assertTrue(comparePossiblyUndefinedValues(0, undefined, cmp) < 0);
    assertTrue(comparePossiblyUndefinedValues(undefined, 0, cmp) > 0);
    assertTrue(comparePossiblyUndefinedValues(undefined, undefined, cmp) == 0);
  });

  test('compareArrays', function() {
    function cmp(x, y) {
      assertNotUndefined(x);
      assertNotUndefined(y);
      return x - y;
    }

    assertTrue(compareArrays([1], [2], cmp) < 0);
    assertTrue(compareArrays([2], [1], cmp) > 0);

    assertTrue(compareArrays([1], [1, 2], cmp) < 0);
    assertTrue(compareArrays([1, 2], [1], cmp) > 0);

    assertTrue(compareArrays([], [1], cmp) < 0);
    assertTrue(compareArrays([1], [], cmp) > 0);

    assertTrue(compareArrays([2], [1], cmp) > 0);

    assertTrue(compareArrays([], [], cmp) == 0);
    assertTrue(compareArrays([1], [1], cmp) == 0);
  });
});