From 177c8bfc78c9e7822d456a3bb5912ab7be40c298 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 18 Feb 2021 03:44:44 +0800 Subject: [PATCH] builtin/array: merge array_eq_test.v into array_test.v (#8796) --- vlib/builtin/array_eq_test.v | 60 --------------------------- vlib/builtin/array_test.v | 80 +++++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 65 deletions(-) delete mode 100644 vlib/builtin/array_eq_test.v diff --git a/vlib/builtin/array_eq_test.v b/vlib/builtin/array_eq_test.v deleted file mode 100644 index 6a68541642..0000000000 --- a/vlib/builtin/array_eq_test.v +++ /dev/null @@ -1,60 +0,0 @@ -struct User { - age int - name string -} - -fn test_eq() { - assert [5, 6, 7] != [6, 7] - assert [`a`, `b`] == [`a`, `b`] - assert [User{age: 22, name: 'bob'}] == [User{age: 22, name: 'bob'}] - assert [{'bob': 22}, {'tom': 33}] == [{'bob': 22}, {'tom': 33}] - assert [[1, 2, 3], [4]] == [[1, 2, 3], [4]] -} - -fn test_fixed_array_eq() { - a1 := [1, 2, 3]! - assert a1 == [1, 2, 3]! - assert a1 != [2, 3, 4]! - - a2 := [[1, 2]!, [3, 4]!]! - assert a2 == [[1, 2]!, [3, 4]!]! - assert a2 != [[3, 4]!, [1, 2]!]! - - a3 := [[1, 2], [3, 4]]! - assert a3 == [[1, 2], [3, 4]]! - assert a3 != [[1, 1], [2, 2]]! - - a4 := [[`a`, `b`], [`c`, `d`]]! - assert a4 == [[`a`, `b`], [`c`, `d`]]! - assert a4 != [[`c`, `a`], [`a`, `b`]]! - - a5 := [['aaa', 'bbb'], ['ccc', 'ddd']]! - assert a5 == [['aaa', 'bbb'], ['ccc', 'ddd']]! - assert a5 != [['abc', 'def'], ['ccc', 'ddd']]! - - a6 := [['aaa', 'bbb']!, ['ccc', 'ddd']!]! - assert a6 == [['aaa', 'bbb']!, ['ccc', 'ddd']!]! - assert a6 != [['aaa', 'bbb']!, ['aaa', 'ddd']!]! - - a7 := [[1, 2]!, [3, 4]!] - assert a7 == [[1, 2]!, [3, 4]!] - assert a7 != [[2, 3]!, [1, 2]!] - - a8 := [['aaa', 'bbb']!, ['ccc', 'ddd']!] - assert a8 == [['aaa', 'bbb']!, ['ccc', 'ddd']!] - assert a8 != [['bbb', 'aaa']!, ['cccc', 'dddd']!] -} - -fn test_fixed_array_literal_eq() { - assert [1, 2, 3]! == [1, 2, 3]! - assert [1, 1, 1]! != [1, 2, 3]! - - assert [[1, 2], [3, 4]]! == [[1, 2], [3, 4]]! - assert [[1, 1], [2, 2]]! != [[1, 2], [3, 4]]! - - assert [[1, 1]!, [2, 2]!]! == [[1, 1]!, [2, 2]!]! - assert [[1, 1]!, [2, 2]!]! != [[1, 2]!, [2, 3]!]! - - assert [[1, 1]!, [2, 2]!] == [[1, 1]!, [2, 2]!] - assert [[1, 1]!, [2, 2]!] != [[1, 2]!, [2, 3]!] -} diff --git a/vlib/builtin/array_test.v b/vlib/builtin/array_test.v index ab4718cc3a..dbac00081c 100644 --- a/vlib/builtin/array_test.v +++ b/vlib/builtin/array_test.v @@ -696,6 +696,76 @@ struct User { name string } +fn test_eq() { + assert [5, 6, 7] != [6, 7] + assert [`a`, `b`] == [`a`, `b`] + assert [User{ + age: 22 + name: 'bob' + }] == [User{ + age: 22 + name: 'bob' + }] + assert [map{ + 'bob': 22 + }, map{ + 'tom': 33 + }] == [map{ + 'bob': 22 + }, map{ + 'tom': 33 + }] + assert [[1, 2, 3], [4]] == [[1, 2, 3], [4]] +} + +fn test_fixed_array_eq() { + a1 := [1, 2, 3]! + assert a1 == [1, 2, 3]! + assert a1 != [2, 3, 4]! + + a2 := [[1, 2]!, [3, 4]!]! + assert a2 == [[1, 2]!, [3, 4]!]! + assert a2 != [[3, 4]!, [1, 2]!]! + + a3 := [[1, 2], [3, 4]]! + assert a3 == [[1, 2], [3, 4]]! + assert a3 != [[1, 1], [2, 2]]! + + a4 := [[`a`, `b`], [`c`, `d`]]! + assert a4 == [[`a`, `b`], [`c`, `d`]]! + assert a4 != [[`c`, `a`], [`a`, `b`]]! + + a5 := [['aaa', 'bbb'], ['ccc', 'ddd']]! + assert a5 == [['aaa', 'bbb'], ['ccc', 'ddd']]! + assert a5 != [['abc', 'def'], ['ccc', 'ddd']]! + + a6 := [['aaa', 'bbb']!, ['ccc', 'ddd']!]! + assert a6 == [['aaa', 'bbb']!, ['ccc', 'ddd']!]! + assert a6 != [['aaa', 'bbb']!, ['aaa', 'ddd']!]! + + a7 := [[1, 2]!, [3, 4]!] + assert a7 == [[1, 2]!, [3, 4]!] + assert a7 != [[2, 3]!, [1, 2]!] + + a8 := [['aaa', 'bbb']!, ['ccc', 'ddd']!] + assert a8 == [['aaa', 'bbb']!, ['ccc', 'ddd']!] + assert a8 != [['bbb', 'aaa']!, ['cccc', 'dddd']!] +} + +fn test_fixed_array_literal_eq() { + assert [1, 2, 3]! == [1, 2, 3]! + assert [1, 1, 1]! != [1, 2, 3]! + + assert [[1, 2], [3, 4]]! == [[1, 2], [3, 4]]! + assert [[1, 1], [2, 2]]! != [[1, 2], [3, 4]]! + + assert [[1, 1]!, [2, 2]!]! == [[1, 1]!, [2, 2]!]! + assert [[1, 1]!, [2, 2]!]! != [[1, 2]!, [2, 3]!]! + + assert [[1, 1]!, [2, 2]!] == [[1, 1]!, [2, 2]!] + assert [[1, 1]!, [2, 2]!] != [[1, 2]!, [2, 3]!] +} + fn test_sort() { mut a := ['hi', '1', '5', '3'] a.sort() @@ -703,7 +773,7 @@ fn test_sort() { assert a[1] == '3' assert a[2] == '5' assert a[3] == 'hi' - // + mut nums := [67, -3, 108, 42, 7] nums.sort() assert nums[0] == -3 @@ -711,14 +781,14 @@ fn test_sort() { assert nums[2] == 42 assert nums[3] == 67 assert nums[4] == 108 - // + nums.sort(a < b) assert nums[0] == -3 assert nums[1] == 7 assert nums[2] == 42 assert nums[3] == 67 assert nums[4] == 108 - // + mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}] users.sort(a.age < b.age) assert users[0].age == 20 @@ -727,12 +797,12 @@ fn test_sort() { assert users[0].name == 'Bob' assert users[1].name == 'Peter' assert users[2].name == 'Alice' - // + users.sort(a.age > b.age) assert users[0].age == 25 assert users[1].age == 22 assert users[2].age == 20 - // + users.sort(a.name < b.name) // Test sorting by string fields // assert users.map(it.name).join(' ') == 'Alice Bob Peter' }