builtin/array: merge array_eq_test.v into array_test.v (#8796)

pull/8811/head
yuyi 2021-02-18 03:44:44 +08:00 committed by GitHub
parent 4fa315edc2
commit 177c8bfc78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 65 deletions

View File

@ -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]!]
}

View File

@ -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'
}