1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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 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() { fn test_sort() {
mut a := ['hi', '1', '5', '3'] mut a := ['hi', '1', '5', '3']
a.sort() a.sort()
@ -703,7 +773,7 @@ fn test_sort() {
assert a[1] == '3' assert a[1] == '3'
assert a[2] == '5' assert a[2] == '5'
assert a[3] == 'hi' assert a[3] == 'hi'
//
mut nums := [67, -3, 108, 42, 7] mut nums := [67, -3, 108, 42, 7]
nums.sort() nums.sort()
assert nums[0] == -3 assert nums[0] == -3
@ -711,14 +781,14 @@ fn test_sort() {
assert nums[2] == 42 assert nums[2] == 42
assert nums[3] == 67 assert nums[3] == 67
assert nums[4] == 108 assert nums[4] == 108
//
nums.sort(a < b) nums.sort(a < b)
assert nums[0] == -3 assert nums[0] == -3
assert nums[1] == 7 assert nums[1] == 7
assert nums[2] == 42 assert nums[2] == 42
assert nums[3] == 67 assert nums[3] == 67
assert nums[4] == 108 assert nums[4] == 108
//
mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}] mut users := [User{22, 'Peter'}, User{20, 'Bob'}, User{25, 'Alice'}]
users.sort(a.age < b.age) users.sort(a.age < b.age)
assert users[0].age == 20 assert users[0].age == 20
@ -727,12 +797,12 @@ fn test_sort() {
assert users[0].name == 'Bob' assert users[0].name == 'Bob'
assert users[1].name == 'Peter' assert users[1].name == 'Peter'
assert users[2].name == 'Alice' assert users[2].name == 'Alice'
//
users.sort(a.age > b.age) users.sort(a.age > b.age)
assert users[0].age == 25 assert users[0].age == 25
assert users[1].age == 22 assert users[1].age == 22
assert users[2].age == 20 assert users[2].age == 20
//
users.sort(a.name < b.name) // Test sorting by string fields users.sort(a.name < b.name) // Test sorting by string fields
// assert users.map(it.name).join(' ') == 'Alice Bob Peter' // assert users.map(it.name).join(' ') == 'Alice Bob Peter'
} }