| 16 | from ccxt.test.exchange.base import test_shared_methods # noqa E402 |
| 17 | |
| 18 | def test_sort(): |
| 19 | exchange = ccxt.Exchange({ |
| 20 | 'id': 'sampleexchange', |
| 21 | }) |
| 22 | # empty array |
| 23 | test_shared_methods.assert_deep_equal(exchange, None, 'sort', exchange.sort([]), []) |
| 24 | # single element |
| 25 | test_shared_methods.assert_deep_equal(exchange, None, 'sort', exchange.sort(['a']), ['a']) |
| 26 | # already sorted (idempotent) |
| 27 | test_shared_methods.assert_deep_equal(exchange, None, 'sort', exchange.sort(['a', 'b', 'c']), ['a', 'b', 'c']) |
| 28 | # duplicates |
| 29 | test_shared_methods.assert_deep_equal(exchange, None, 'sort', exchange.sort(['b', 'a', 'b', 'c']), ['a', 'b', 'b', 'c']) |
| 30 | test_shared_methods.assert_deep_equal(exchange, None, 'sort', exchange.sort(['b', 'a', 'c', 'd']), ['a', 'b', 'c', 'd']) |
| 31 | # todo 1: atm, `sort` is only meant for strings. we should update to support numerics |
| 32 | # todo 2: add test for above 10, eg: 1, 2, 10, 20, 21 |
| 33 | # # integers (single-digit, safe for cross-language lexicographic/numeric consistency) |
| 34 | # testSharedMethods.assertDeepEqual (exchange, undefined, 'sort', exchange.sort ([ 3, 1, 2 ]), [ 1, 2, 3 ]); |
| 35 | # testSharedMethods.assertDeepEqual (exchange, undefined, 'sort', exchange.sort ([ 5, 3, 1, 4, 2 ]), [ 1, 2, 3, 4, 5 ]); |
| 36 | # testSharedMethods.assertDeepEqual (exchange, undefined, 'sort', exchange.sort ([ 0, 3, 1, 2 ]), [ 0, 1, 2, 3 ]); |
| 37 | # # floats (values chosen so lexicographic order matches numeric order) |
| 38 | # testSharedMethods.assertDeepEqual (exchange, undefined, 'sort', exchange.sort ([ 1.5, 0.5, 2.5 ]), [ 0.5, 1.5, 2.5 ]); |
| 39 | # testSharedMethods.assertDeepEqual (exchange, undefined, 'sort', exchange.sort ([ 3.3, 1.1, 2.2 ]), [ 1.1, 2.2, 3.3 ]); |
| 40 | # immutability - original array should not be modified |
| 41 | original = ['b', 'a', 'c'] |
| 42 | exchange.sort(original) |
| 43 | test_shared_methods.assert_deep_equal(exchange, None, 'sort', original, ['b', 'a', 'c']) |