2020-12-06 01:28:11 +01:00
|
|
|
import rand
|
|
|
|
|
|
|
|
const (
|
|
|
|
strings = unique_strings(20000, 10)
|
|
|
|
)
|
|
|
|
|
|
|
|
fn unique_strings(arr_len int, str_len int) []string {
|
2020-12-13 02:05:56 +01:00
|
|
|
mut arr := []string{cap: arr_len}
|
2020-12-06 01:28:11 +01:00
|
|
|
for arr.len < arr_len {
|
|
|
|
str := rand.string(str_len)
|
|
|
|
if str !in arr {
|
|
|
|
arr << str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_get_and_set_many() {
|
|
|
|
mut m := map[string]int{}
|
|
|
|
for i, s in strings {
|
|
|
|
m[s] = i
|
|
|
|
assert m[s] == i
|
|
|
|
assert m.len == i + 1
|
|
|
|
}
|
|
|
|
for i, s in strings {
|
|
|
|
assert m[s] == i
|
|
|
|
}
|
|
|
|
assert m.len == strings.len
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_for_in_many() {
|
|
|
|
mut m := map[string]int{}
|
|
|
|
for i, s in strings {
|
|
|
|
m[s] = i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert m[k] == v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_keys_many() {
|
|
|
|
mut m := map[string]int{}
|
|
|
|
for i, s in strings {
|
|
|
|
m[s] = i
|
|
|
|
}
|
|
|
|
keys := m.keys()
|
|
|
|
assert keys.len == strings.len
|
|
|
|
assert keys.len == m.len
|
|
|
|
assert keys == strings
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_deletes_many() {
|
|
|
|
mut m := map[string]int{}
|
|
|
|
for i, s in strings {
|
|
|
|
m[s] = i
|
|
|
|
}
|
|
|
|
for i, s in strings {
|
|
|
|
m.delete(s)
|
|
|
|
assert m[s] == 0
|
|
|
|
assert m.len == strings.len - (i + 1)
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
assert m.keys().len == 0
|
|
|
|
}
|
2019-08-31 15:24:37 +02:00
|
|
|
|
2019-06-30 22:44:15 +02:00
|
|
|
struct User {
|
2020-10-31 13:00:04 +01:00
|
|
|
mut:
|
2019-08-31 15:24:37 +02:00
|
|
|
name string
|
|
|
|
}
|
2019-06-30 22:44:15 +02:00
|
|
|
|
2020-05-27 18:12:34 +02:00
|
|
|
struct Aaa {
|
2020-04-27 22:53:26 +02:00
|
|
|
mut:
|
2020-12-06 01:28:11 +01:00
|
|
|
m map[string]int
|
2019-08-31 15:24:37 +02:00
|
|
|
users map[string]User
|
2019-06-30 13:44:08 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 18:12:34 +02:00
|
|
|
fn (mut a Aaa) set(key string, val int) {
|
2019-06-30 13:44:08 +02:00
|
|
|
a.m[key] = val
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m := map[string]int{}
|
2020-06-21 16:51:02 +02:00
|
|
|
assert m.len == 0
|
2019-06-30 13:44:08 +02:00
|
|
|
m['hi'] = 80
|
2019-08-31 15:24:37 +02:00
|
|
|
m['hello'] = 101
|
2019-06-30 13:44:08 +02:00
|
|
|
assert m['hi'] == 80
|
2019-08-31 15:24:37 +02:00
|
|
|
assert m['hello'] == 101
|
2020-06-21 16:51:02 +02:00
|
|
|
assert m.len == 2
|
2019-08-31 15:24:37 +02:00
|
|
|
assert 'hi' in m
|
2019-07-14 11:01:32 +02:00
|
|
|
mut sum := 0
|
2019-08-31 15:24:37 +02:00
|
|
|
// Test `for in`
|
2020-04-27 22:53:26 +02:00
|
|
|
for _, val in m {
|
2019-08-31 15:24:37 +02:00
|
|
|
sum += val
|
|
|
|
}
|
|
|
|
assert sum == 80 + 101
|
|
|
|
// Test `.keys()`
|
|
|
|
keys := m.keys()
|
|
|
|
assert keys.len == 2
|
2020-01-24 20:13:59 +01:00
|
|
|
assert 'hi' in keys
|
|
|
|
assert 'hello' in keys
|
2019-08-31 15:24:37 +02:00
|
|
|
m.delete('hi')
|
2020-06-21 16:51:02 +02:00
|
|
|
assert m.len == 1
|
2019-10-30 18:19:59 +01:00
|
|
|
m.delete('aloha')
|
2020-06-21 16:51:02 +02:00
|
|
|
assert m.len == 1
|
2019-07-30 21:27:31 +02:00
|
|
|
assert m['hi'] == 0
|
2019-08-31 15:24:37 +02:00
|
|
|
assert m.keys().len == 1
|
|
|
|
assert m.keys()[0] == 'hello'
|
2020-12-06 01:28:11 +01:00
|
|
|
// //
|
|
|
|
mut users := map[string]User{}
|
2019-08-31 15:24:37 +02:00
|
|
|
users['1'] = User{'Peter'}
|
2019-06-30 22:44:15 +02:00
|
|
|
peter := users['1']
|
2020-12-06 01:28:11 +01:00
|
|
|
assert peter.name == 'Peter'
|
2020-05-27 18:12:34 +02:00
|
|
|
mut a := Aaa{
|
2020-12-06 01:28:11 +01:00
|
|
|
m: map[string]int{}
|
|
|
|
users: map[string]User{}
|
2019-06-30 13:44:08 +02:00
|
|
|
}
|
2019-08-31 15:24:37 +02:00
|
|
|
a.users['Bob'] = User{'Bob'}
|
|
|
|
q := a.users['Bob']
|
|
|
|
assert q.name == 'Bob'
|
2020-10-31 13:00:04 +01:00
|
|
|
// test struct field change
|
|
|
|
a.users['Bob'].name = 'bob'
|
|
|
|
q2 := a.users['Bob']
|
|
|
|
assert q2.name == 'bob'
|
2019-06-30 13:44:08 +02:00
|
|
|
a.m['one'] = 1
|
|
|
|
a.set('two', 2)
|
|
|
|
assert a.m['one'] == 1
|
|
|
|
assert a.m['two'] == 2
|
|
|
|
}
|
2019-07-14 11:01:32 +02:00
|
|
|
|
2019-08-03 09:44:08 +02:00
|
|
|
fn test_map_init() {
|
2021-02-06 22:13:24 +01:00
|
|
|
one := 'one'
|
|
|
|
three := 'three'
|
|
|
|
m := map{
|
2021-02-21 10:54:30 +01:00
|
|
|
one: 1
|
2020-12-06 01:28:11 +01:00
|
|
|
'two': 2
|
2021-02-06 22:13:24 +01:00
|
|
|
three: 1 + 2
|
2020-12-06 01:28:11 +01:00
|
|
|
}
|
2019-08-31 15:24:37 +02:00
|
|
|
assert m['one'] == 1
|
|
|
|
assert m['two'] == 2
|
2021-02-06 22:13:24 +01:00
|
|
|
assert m['three'] == 3
|
|
|
|
assert m['unknown'] == 0
|
2019-08-31 15:24:37 +02:00
|
|
|
}
|
2019-08-03 09:44:08 +02:00
|
|
|
|
2019-07-14 11:01:32 +02:00
|
|
|
fn test_string_map() {
|
2020-12-06 01:28:11 +01:00
|
|
|
// m := map[string]Fn
|
2019-08-31 15:24:37 +02:00
|
|
|
}
|
2019-07-14 11:01:32 +02:00
|
|
|
|
2019-08-31 15:24:37 +02:00
|
|
|
fn test_large_map() {
|
2020-12-06 01:28:11 +01:00
|
|
|
// ticks := time.ticks()
|
|
|
|
mut nums := map[string]int{}
|
2020-04-02 23:42:08 +02:00
|
|
|
n := 30 * 1000
|
2020-12-06 01:28:11 +01:00
|
|
|
for i in 0 .. n {
|
|
|
|
key := i.str()
|
|
|
|
nums[key] = i
|
2019-07-14 11:01:32 +02:00
|
|
|
}
|
2019-08-31 15:24:37 +02:00
|
|
|
assert nums['1'] == 1
|
|
|
|
assert nums['999'] == 999
|
|
|
|
assert nums['1000000'] == 0
|
2020-12-06 01:28:11 +01:00
|
|
|
// println(time.ticks() - ticks)
|
2019-08-31 15:24:37 +02:00
|
|
|
}
|
2019-07-15 12:33:18 +02:00
|
|
|
|
|
|
|
fn test_various_map_value() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m1 := map[string]int{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m1['test'] = 1
|
|
|
|
assert m1['test'] == 1
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m2 := map[string]string{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m2['test'] = 'test'
|
|
|
|
assert m2['test'] == 'test'
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m3 := map[string]i8{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m3['test'] = i8(0)
|
|
|
|
assert m3['test'] == i8(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m4 := map[string]i16{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m4['test'] = i16(0)
|
|
|
|
assert m4['test'] == i16(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m7 := map[string]u16{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m7['test'] = u16(0)
|
|
|
|
assert m7['test'] == u16(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m8 := map[string]u32{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m8['test'] = u32(0)
|
|
|
|
assert m8['test'] == u32(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m9 := map[string]bool{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m9['test'] = true
|
|
|
|
assert m9['test'] == true
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m10 := map[string]byte{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m10['test'] = byte(0)
|
|
|
|
assert m10['test'] == byte(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m11 := map[string]f32{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m11['test'] = f32(0.0)
|
|
|
|
assert m11['test'] == f32(0.0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m12 := map[string]f64{}
|
2019-07-15 12:33:18 +02:00
|
|
|
m12['test'] = f64(0.0)
|
|
|
|
assert m12['test'] == f64(0.0)
|
2020-12-06 01:28:11 +01:00
|
|
|
// mut m13 := map[string]rune
|
|
|
|
// m13['test'] = rune(0)
|
|
|
|
// assert m13['test'] == rune(0)
|
|
|
|
mut m14 := map[string]voidptr{}
|
2019-12-21 23:44:16 +01:00
|
|
|
m14['test'] = voidptr(0)
|
|
|
|
assert m14['test'] == voidptr(0)
|
2021-04-05 20:21:46 +02:00
|
|
|
mut m15 := map[string]&byte{}
|
|
|
|
m15['test'] = &byte(0)
|
|
|
|
assert m15['test'] == &byte(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m16 := map[string]i64{}
|
2019-07-18 04:32:49 +02:00
|
|
|
m16['test'] = i64(0)
|
|
|
|
assert m16['test'] == i64(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m17 := map[string]u64{}
|
2019-07-18 04:32:49 +02:00
|
|
|
m17['test'] = u64(0)
|
|
|
|
assert m17['test'] == u64(0)
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m18 := map[string]&int{}
|
2019-12-21 23:44:16 +01:00
|
|
|
m18['test'] = &int(0)
|
|
|
|
assert m18['test'] == &int(0)
|
2019-07-15 12:33:18 +02:00
|
|
|
}
|
2019-08-16 17:44:35 +02:00
|
|
|
|
|
|
|
fn test_string_arr() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m := map[string][]string{}
|
2019-08-31 15:24:37 +02:00
|
|
|
m['a'] = ['one', 'two']
|
|
|
|
assert m['a'].len == 2
|
|
|
|
assert m['a'][0] == 'one'
|
|
|
|
assert m['a'][1] == 'two'
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn mut_map(mut m map[string]int) {
|
2019-11-12 20:35:53 +01:00
|
|
|
m['a'] = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_mut_arg() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m := map[string]int{}
|
2019-11-12 20:35:53 +01:00
|
|
|
mut_map(mut m)
|
2020-04-02 23:42:08 +02:00
|
|
|
a := m['a']
|
|
|
|
assert a == 10
|
2019-11-12 20:35:53 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 18:05:27 +01:00
|
|
|
fn test_delete() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut m := map[string]int{}
|
2019-11-22 18:05:27 +01:00
|
|
|
m['one'] = 1
|
|
|
|
m['two'] = 2
|
|
|
|
println(m['two']) // => "2"
|
|
|
|
m.delete('two')
|
2020-04-02 23:42:08 +02:00
|
|
|
println(m['two'].str()) // => 0
|
|
|
|
assert ('two' in m) == false
|
2019-11-22 18:05:27 +01:00
|
|
|
println('two' in m) // => true, on Linux and Windows <-- wrong !
|
2019-12-19 19:17:12 +01:00
|
|
|
}
|
2019-11-22 18:05:27 +01:00
|
|
|
|
2019-12-19 19:17:12 +01:00
|
|
|
fn test_delete_size() {
|
2020-12-06 01:28:11 +01:00
|
|
|
arr := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
|
|
|
mut m := map[string]int{}
|
|
|
|
for _ in 0 .. 10 {
|
|
|
|
for i in 0 .. 10 {
|
|
|
|
m[arr[i]] = i
|
|
|
|
}
|
2021-01-30 11:55:10 +01:00
|
|
|
assert m.len == 10
|
2020-12-06 01:28:11 +01:00
|
|
|
println(m.len)
|
|
|
|
for i in 0 .. 10 {
|
|
|
|
m.delete(arr[i])
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 19:17:12 +01:00
|
|
|
}
|
2020-06-19 15:00:27 +02:00
|
|
|
|
2021-03-17 01:42:33 +01:00
|
|
|
fn test_nested_for_in() {
|
|
|
|
mut m := map[string]int{}
|
|
|
|
for i in 0 .. 1000 {
|
|
|
|
m[i.str()] = i
|
|
|
|
}
|
|
|
|
mut i := 0
|
|
|
|
for key1, _ in m {
|
|
|
|
assert key1 == i.str()
|
|
|
|
i++
|
|
|
|
mut j := 0
|
|
|
|
for key2, _ in m {
|
|
|
|
assert key2 == j.str()
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_delete_in_for_in() {
|
|
|
|
mut m := map[string]string{}
|
|
|
|
for i in 0 .. 1000 {
|
|
|
|
m[i.str()] = i.str()
|
|
|
|
}
|
|
|
|
mut i := 0
|
|
|
|
for key, _ in m {
|
|
|
|
assert key == i.str()
|
|
|
|
m.delete(key)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
assert m.str() == '{}'
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_set_in_for_in() {
|
|
|
|
mut m := map[string]string{}
|
|
|
|
for i in 0 .. 10 {
|
|
|
|
m[i.str()] = i.str()
|
|
|
|
}
|
|
|
|
mut last_key := ''
|
|
|
|
mut i := 0
|
|
|
|
for key, _ in m {
|
|
|
|
m['10'] = '10'
|
|
|
|
assert key == i.str()
|
|
|
|
last_key = key
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
assert last_key == '10'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_delete_and_set_in_for_in() {
|
|
|
|
mut m := map[string]string{}
|
|
|
|
for i in 0 .. 1000 {
|
|
|
|
m[i.str()] = i.str()
|
|
|
|
}
|
|
|
|
mut i := 0
|
|
|
|
for key, _ in m {
|
|
|
|
assert key == i.str()
|
|
|
|
m.delete(key)
|
|
|
|
m[key] = i.str()
|
|
|
|
if i == 999 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
assert m.len == 1000
|
|
|
|
i = 0
|
|
|
|
for key, _ in m {
|
|
|
|
assert m[key] == i.str()
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
assert i == 1000
|
|
|
|
}
|
|
|
|
|
2020-06-19 15:00:27 +02:00
|
|
|
struct Mstruct1 {
|
|
|
|
pub mut:
|
|
|
|
mymap map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Mstruct2 {
|
|
|
|
pub mut:
|
|
|
|
mymap map[string]f64
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Mstruct3 {
|
|
|
|
pub mut:
|
|
|
|
mymap map[string]u16
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map_assign() {
|
|
|
|
mut a := map[string]f64{}
|
|
|
|
mut b := map[string]int{}
|
|
|
|
mut c := map[string]u16{}
|
2021-02-08 15:57:42 +01:00
|
|
|
a = map{
|
2020-06-19 15:00:27 +02:00
|
|
|
'x': 12.4
|
|
|
|
'y': 3
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
b = map{
|
2020-06-19 15:00:27 +02:00
|
|
|
'u': -13
|
|
|
|
'v': 12
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
c = map{
|
2020-06-19 15:00:27 +02:00
|
|
|
's': u16(5)
|
|
|
|
't': 3
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
_ := Mstruct1{map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'p': 12
|
|
|
|
}}
|
2021-02-08 15:57:42 +01:00
|
|
|
_ := Mstruct2{map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'q': 1.7
|
|
|
|
}}
|
2021-02-08 15:57:42 +01:00
|
|
|
_ := Mstruct3{map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'r': u16(6)
|
|
|
|
's': 5
|
|
|
|
}}
|
2020-06-19 15:00:27 +02:00
|
|
|
}
|
2020-06-24 20:41:26 +02:00
|
|
|
|
|
|
|
fn test_postfix_op_directly() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut a := map[string]int{}
|
2020-06-24 20:41:26 +02:00
|
|
|
a['aaa']++
|
|
|
|
assert a['aaa'] == 1
|
|
|
|
a['aaa']++
|
|
|
|
assert a['aaa'] == 2
|
|
|
|
a['bbb']--
|
|
|
|
assert a['bbb'] == -1
|
|
|
|
a['bbb']--
|
|
|
|
assert a['bbb'] == -2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map_push_directly() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut a := map[string][]string{}
|
2020-06-24 20:41:26 +02:00
|
|
|
a['aaa'] << ['a', 'b', 'c']
|
|
|
|
assert a['aaa'].len == 3
|
|
|
|
assert a['aaa'] == ['a', 'b', 'c']
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_assign_directly() {
|
2020-12-06 01:28:11 +01:00
|
|
|
mut a := map[string]int{}
|
2020-06-24 20:41:26 +02:00
|
|
|
a['aaa'] += 4
|
|
|
|
assert a['aaa'] == 4
|
|
|
|
a['aaa'] -= 2
|
|
|
|
assert a['aaa'] == 2
|
|
|
|
}
|
2020-06-25 12:53:12 +02:00
|
|
|
|
|
|
|
fn test_map_in_directly() {
|
2021-03-17 01:42:33 +01:00
|
|
|
for k, v in map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'aa': 1
|
|
|
|
} {
|
2020-06-25 12:53:12 +02:00
|
|
|
assert k == 'aa'
|
|
|
|
assert v == 1
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 13:34:59 +02:00
|
|
|
|
|
|
|
fn test_plus_assign_string() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut m := map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'one': ''
|
|
|
|
}
|
2020-06-28 13:34:59 +02:00
|
|
|
m['one'] += '1'
|
|
|
|
assert m.len == 1
|
|
|
|
assert m['one'] == '1'
|
|
|
|
}
|
2020-07-01 10:48:46 +02:00
|
|
|
|
|
|
|
fn test_map_keys_to_array() {
|
2021-02-08 15:57:42 +01:00
|
|
|
m := map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'a': 'b'
|
|
|
|
'c': 'd'
|
|
|
|
}
|
2020-07-01 10:48:46 +02:00
|
|
|
mut arr := []string{}
|
|
|
|
for k, _ in m {
|
|
|
|
arr << k
|
|
|
|
}
|
|
|
|
sarr := arr.str()
|
|
|
|
println(sarr)
|
|
|
|
assert sarr == "['a', 'c']"
|
|
|
|
}
|
2020-07-03 16:33:53 +02:00
|
|
|
|
|
|
|
fn map_in_mut(mut m map[string]int) {
|
|
|
|
if 'one' in m {
|
|
|
|
m['one'] = 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map_in_mut() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut m := map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'one': 1
|
|
|
|
}
|
2020-07-03 16:33:53 +02:00
|
|
|
map_in_mut(mut m)
|
|
|
|
assert m['one'] == 2
|
|
|
|
}
|
2020-07-03 18:07:11 +02:00
|
|
|
|
2020-12-28 18:32:51 +01:00
|
|
|
fn test_map_in() {
|
2021-02-08 15:57:42 +01:00
|
|
|
m := map{
|
2020-12-28 18:32:51 +01:00
|
|
|
'Foo': 'bar'
|
|
|
|
}
|
|
|
|
if 'foo'.capitalize() in m {
|
|
|
|
println('ok')
|
|
|
|
} else {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:07:11 +02:00
|
|
|
fn mut_map_with_relation_op_in_fn(mut m map[string]int) {
|
|
|
|
if m['one'] == 1 {
|
|
|
|
m['three'] = 3
|
|
|
|
}
|
|
|
|
if m['two'] != 1 {
|
2020-10-31 13:00:04 +01:00
|
|
|
m['four'] = 4
|
2020-07-03 18:07:11 +02:00
|
|
|
}
|
|
|
|
if m['one'] > 0 {
|
2020-10-31 13:00:04 +01:00
|
|
|
m['five'] = 5
|
2020-07-03 18:07:11 +02:00
|
|
|
}
|
|
|
|
if m['one'] < 2 {
|
2020-10-31 13:00:04 +01:00
|
|
|
m['six'] = 6
|
2020-07-03 18:07:11 +02:00
|
|
|
}
|
|
|
|
if m['two'] >= 2 {
|
2020-10-31 13:00:04 +01:00
|
|
|
m['seven'] = 7
|
2020-07-03 18:07:11 +02:00
|
|
|
}
|
|
|
|
if m['two'] <= 2 {
|
2020-10-31 13:00:04 +01:00
|
|
|
m['eight'] = 8
|
2020-07-03 18:07:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_mut_map_with_relation_op_in_fn() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut m := map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'one': 1
|
|
|
|
'two': 2
|
|
|
|
}
|
2020-07-03 18:07:11 +02:00
|
|
|
mut_map_with_relation_op_in_fn(mut m)
|
|
|
|
assert 'three' in m
|
|
|
|
assert 'four' in m
|
|
|
|
assert 'five' in m
|
|
|
|
assert 'six' in m
|
|
|
|
assert 'seven' in m
|
|
|
|
assert 'eight' in m
|
|
|
|
}
|
2020-11-13 12:37:11 +01:00
|
|
|
|
|
|
|
fn test_map_str_after_delete() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut m := map{
|
2020-12-19 10:28:17 +01:00
|
|
|
'first': 1
|
2020-12-06 01:28:11 +01:00
|
|
|
'second': 2
|
2020-12-19 10:28:17 +01:00
|
|
|
'third': 3
|
2020-11-13 12:37:11 +01:00
|
|
|
}
|
|
|
|
osm := '$m'
|
|
|
|
m.delete('second')
|
|
|
|
nsm := '$m'
|
2020-12-06 01:28:11 +01:00
|
|
|
println('m: $m')
|
2020-11-13 12:37:11 +01:00
|
|
|
assert osm == "{'first': 1, 'second': 2, 'third': 3}"
|
|
|
|
assert nsm == "{'first': 1, 'third': 3}"
|
|
|
|
}
|
2020-12-03 00:40:11 +01:00
|
|
|
|
|
|
|
fn test_modify_map_value() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut m1 := map{
|
2020-12-06 01:28:11 +01:00
|
|
|
'foo': 3
|
|
|
|
'bar': -7
|
|
|
|
}
|
2020-12-03 00:40:11 +01:00
|
|
|
m1['foo'] += 5
|
|
|
|
m1['bar'] *= -2
|
|
|
|
assert m1['foo'] == 8
|
|
|
|
assert m1['bar'] == 14
|
|
|
|
}
|
2020-12-17 08:44:50 +01:00
|
|
|
|
|
|
|
fn test_map_clone() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut nums := map{
|
2020-12-19 10:28:17 +01:00
|
|
|
'foo': 1
|
2020-12-17 08:44:50 +01:00
|
|
|
'bar': 2
|
|
|
|
}
|
|
|
|
mut nums2 := nums.clone()
|
|
|
|
nums2['foo']++
|
|
|
|
nums2['bar'] *= 4
|
|
|
|
assert nums['foo'] == 1
|
|
|
|
assert nums['bar'] == 2
|
|
|
|
assert nums2['foo'] == 2
|
|
|
|
assert nums2['bar'] == 8
|
|
|
|
}
|
2020-12-19 06:55:13 +01:00
|
|
|
|
|
|
|
struct MValue {
|
2020-12-19 10:28:17 +01:00
|
|
|
name string
|
|
|
|
misc map[string]string
|
2020-12-19 06:55:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map_default_zero() {
|
2020-12-19 10:28:17 +01:00
|
|
|
m := map[string]MValue{}
|
|
|
|
v := m['unknown']
|
|
|
|
x := v.misc['x']
|
|
|
|
println(x)
|
2020-12-19 06:55:13 +01:00
|
|
|
assert x == ''
|
|
|
|
}
|
2020-12-19 10:28:17 +01:00
|
|
|
|
|
|
|
fn test_map_or() {
|
2021-02-08 15:57:42 +01:00
|
|
|
m := map{
|
2020-12-19 10:28:17 +01:00
|
|
|
'first': 1
|
|
|
|
'second': 2
|
|
|
|
'third': 3
|
|
|
|
}
|
2020-12-21 14:37:19 +01:00
|
|
|
_ = m
|
2020-12-19 10:28:17 +01:00
|
|
|
// num := m['first'] or { return }
|
|
|
|
}
|
2020-12-27 14:18:46 +01:00
|
|
|
|
|
|
|
fn test_int_keys() {
|
2020-12-28 11:22:14 +01:00
|
|
|
mut m := map[int]int{}
|
2020-12-27 14:18:46 +01:00
|
|
|
m[3] = 9
|
|
|
|
m[4] = 16
|
|
|
|
assert m.len == 2
|
|
|
|
assert m[3] == 9
|
|
|
|
assert m[4] == 16
|
|
|
|
m[5] += 24
|
|
|
|
m[5]++
|
|
|
|
assert m[5] == 25
|
2021-04-04 01:11:47 +02:00
|
|
|
mut m2 := map{
|
2021-01-30 11:55:10 +01:00
|
|
|
3: 9
|
|
|
|
4: 16
|
|
|
|
5: 25
|
|
|
|
}
|
2021-04-04 01:11:47 +02:00
|
|
|
|
|
|
|
four := 4
|
2021-04-07 14:12:12 +02:00
|
|
|
m2.delete(3)
|
|
|
|
m2.delete(four)
|
|
|
|
m2.delete(5)
|
2021-04-04 01:11:47 +02:00
|
|
|
assert m2.len == 0
|
|
|
|
assert m2[3] == 0
|
|
|
|
assert m2[4] == 0
|
|
|
|
assert m2[5] == 0
|
|
|
|
assert m2.keys() == []
|
|
|
|
|
|
|
|
m2 = map{
|
|
|
|
3: 9
|
|
|
|
4: 16
|
|
|
|
5: 25
|
|
|
|
}
|
|
|
|
|
2021-01-01 17:23:32 +01:00
|
|
|
assert m2.len == 3
|
2021-01-01 20:54:49 +01:00
|
|
|
// clone
|
2020-12-27 14:18:46 +01:00
|
|
|
mc := m.clone()
|
2021-01-01 20:54:49 +01:00
|
|
|
same := mc == m
|
|
|
|
assert same
|
2020-12-27 14:18:46 +01:00
|
|
|
assert mc.len == 3
|
2021-01-30 11:55:10 +01:00
|
|
|
assert mc.keys() == [3, 4, 5]
|
2020-12-27 14:18:46 +01:00
|
|
|
mut all := []int{}
|
|
|
|
for k, v in mc {
|
|
|
|
assert m[k] == v
|
|
|
|
all << k
|
|
|
|
all << v
|
|
|
|
}
|
2020-12-28 11:22:14 +01:00
|
|
|
assert all == [3, 9, 4, 16, 5, 25]
|
2021-04-04 01:11:47 +02:00
|
|
|
|
|
|
|
mut m3 := map{
|
|
|
|
1: 'one'
|
|
|
|
2: 'two'
|
|
|
|
}
|
|
|
|
assert m3[1] == 'one'
|
2021-04-07 14:12:12 +02:00
|
|
|
m3.delete(1)
|
2020-12-27 14:18:46 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 06:45:21 +02:00
|
|
|
enum Color {
|
|
|
|
red
|
|
|
|
green
|
|
|
|
blue
|
|
|
|
}
|
|
|
|
|
|
|
|
type ColorAlias = Color
|
|
|
|
|
|
|
|
fn test_alias_enum() {
|
|
|
|
mut m := map[ColorAlias]string{}
|
|
|
|
m[Color.red] = 'hi'
|
2021-04-28 21:11:32 +02:00
|
|
|
assert m[Color.red] == 'hi'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_enum_in_map() {
|
|
|
|
mut m := map[Color]string{}
|
|
|
|
m[Color.red] = 'hi'
|
|
|
|
assert Color.red in m
|
|
|
|
assert Color.green !in m
|
|
|
|
assert Color.blue !in m
|
2021-04-28 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 14:18:46 +01:00
|
|
|
fn test_voidptr_keys() {
|
2020-12-28 11:22:14 +01:00
|
|
|
mut m := map[voidptr]string{}
|
2020-12-27 14:18:46 +01:00
|
|
|
v := 5
|
|
|
|
m[&v] = 'var'
|
|
|
|
m[&m] = 'map'
|
|
|
|
assert m[&v] == 'var'
|
|
|
|
assert m[&m] == 'map'
|
|
|
|
assert m.len == 2
|
|
|
|
}
|
2020-12-28 11:22:14 +01:00
|
|
|
|
2021-01-01 17:23:32 +01:00
|
|
|
fn test_rune_keys() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut m := map{
|
2021-01-30 11:55:10 +01:00
|
|
|
`!`: 2
|
|
|
|
`%`: 3
|
|
|
|
}
|
2021-01-01 17:23:32 +01:00
|
|
|
assert typeof(m).name == 'map[rune]int'
|
|
|
|
assert m[`!`] == 2
|
|
|
|
m[`@`] = 7
|
|
|
|
assert m.len == 3
|
2021-01-02 19:25:46 +01:00
|
|
|
println(m)
|
|
|
|
assert '$m' == '{`!`: 2, `%`: 3, `@`: 7}'
|
2021-01-30 11:55:10 +01:00
|
|
|
|
2021-01-01 17:23:32 +01:00
|
|
|
mut a := []rune{}
|
|
|
|
for k, v in m {
|
|
|
|
a << k
|
|
|
|
a << rune(v) + `0`
|
|
|
|
}
|
|
|
|
assert a == [`!`, `2`, `%`, `3`, `@`, `7`]
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:22:14 +01:00
|
|
|
fn test_eq() {
|
2021-02-08 15:57:42 +01:00
|
|
|
a := map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'a': 1
|
|
|
|
'b': 2
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
assert a == map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'a': 1
|
|
|
|
'b': 2
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
b := map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'a': [[1]]
|
|
|
|
'b': [[2]]
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
assert b == map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'a': [[1]]
|
|
|
|
'b': [[2]]
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
c := map{
|
|
|
|
'a': map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'11': 1
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
'b': map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'22': 2
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
assert c == map{
|
|
|
|
'a': map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'11': 1
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
'b': map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'22': 2
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
d := map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'a': MValue{
|
|
|
|
name: 'aa'
|
2021-02-08 15:57:42 +01:00
|
|
|
misc: map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'11': '1'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'b': MValue{
|
|
|
|
name: 'bb'
|
2021-02-08 15:57:42 +01:00
|
|
|
misc: map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'22': '2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
assert d == map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'a': MValue{
|
|
|
|
name: 'aa'
|
2021-02-08 15:57:42 +01:00
|
|
|
misc: map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'11': '1'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'b': MValue{
|
|
|
|
name: 'bb'
|
2021-02-08 15:57:42 +01:00
|
|
|
misc: map{
|
2020-12-28 11:22:14 +01:00
|
|
|
'22': '2'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 19:25:46 +01:00
|
|
|
|
|
|
|
fn test_non_string_key_map_str() {
|
2021-02-08 15:57:42 +01:00
|
|
|
assert map{
|
2021-01-30 11:55:10 +01:00
|
|
|
23: 4
|
|
|
|
}.str() == '{23: 4}'
|
2021-02-08 15:57:42 +01:00
|
|
|
assert map{
|
2021-01-30 11:55:10 +01:00
|
|
|
`a`: 12
|
|
|
|
`b`: 13
|
|
|
|
}.str() == '{`a`: 12, `b`: 13}'
|
2021-02-08 15:57:42 +01:00
|
|
|
assert map{
|
2021-01-30 11:55:10 +01:00
|
|
|
23: 'foo'
|
|
|
|
25: 'bar'
|
|
|
|
}.str() == "{23: 'foo', 25: 'bar'}"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_map_assign_empty_map_init() {
|
2021-02-08 15:57:42 +01:00
|
|
|
mut a := map{
|
2021-01-30 11:55:10 +01:00
|
|
|
'one': 1
|
|
|
|
}
|
2021-02-08 15:57:42 +01:00
|
|
|
a = map{}
|
2021-01-30 11:55:10 +01:00
|
|
|
println(a)
|
|
|
|
assert a == map[string]int{}
|
|
|
|
assert '$a' == '{}'
|
2021-01-02 19:25:46 +01:00
|
|
|
}
|
2021-02-21 10:54:30 +01:00
|
|
|
|
|
|
|
fn test_in_map_literal() {
|
|
|
|
assert 1 in map{
|
|
|
|
1: 'one'
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 13:26:20 +02:00
|
|
|
|
|
|
|
fn test_byte_keys() {
|
|
|
|
mut m := map[byte]byte{}
|
|
|
|
byte_max := byte(255)
|
|
|
|
for i in byte(0) .. byte_max {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in byte(0) .. 100 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == byte_max
|
|
|
|
keys := m.keys()
|
|
|
|
for i in byte(0) .. byte_max {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in byte(0) .. byte_max {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_i16_keys() {
|
|
|
|
mut m := map[i16]i16{}
|
|
|
|
end := i16(1000)
|
|
|
|
for i in i16(0) .. end {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in i16(0) .. 500 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == end
|
|
|
|
keys := m.keys()
|
|
|
|
for i in i16(0) .. end {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in i16(0) .. end {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_u16_keys() {
|
|
|
|
mut m := map[u16]u16{}
|
|
|
|
end := u16(1000)
|
|
|
|
for i in u16(0) .. end {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in u16(0) .. 500 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == end
|
|
|
|
keys := m.keys()
|
|
|
|
for i in u16(0) .. end {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in u16(0) .. end {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_u32_keys() {
|
|
|
|
mut m := map[u32]u32{}
|
|
|
|
end := u32(1000)
|
|
|
|
for i in u32(0) .. end {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in u32(0) .. 500 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == end
|
|
|
|
keys := m.keys()
|
|
|
|
for i in u32(0) .. end {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in u32(0) .. end {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_int_keys2() {
|
|
|
|
mut m := map[int]int{}
|
|
|
|
end := 1000
|
|
|
|
for i in int(0) .. end {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in int(0) .. 500 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == end
|
|
|
|
keys := m.keys()
|
|
|
|
for i in int(0) .. end {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in int(0) .. end {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_i64_keys() {
|
|
|
|
mut m := map[i64]i64{}
|
|
|
|
end := i64(1000)
|
|
|
|
for i in i64(0) .. end {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in i64(0) .. 500 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == end
|
|
|
|
keys := m.keys()
|
|
|
|
for i in i64(0) .. end {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in i64(0) .. end {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_u64_keys() {
|
|
|
|
mut m := map[u64]u64{}
|
|
|
|
end := u64(1000)
|
|
|
|
for i in u64(0) .. end {
|
|
|
|
m[i] = i
|
|
|
|
assert m[i] == i
|
|
|
|
}
|
|
|
|
for k, v in m {
|
|
|
|
assert k == v
|
|
|
|
}
|
|
|
|
for i in u64(0) .. 500 {
|
|
|
|
m[i]++
|
|
|
|
assert m[i] == i + 1
|
|
|
|
}
|
|
|
|
assert m.len == end
|
|
|
|
keys := m.keys()
|
|
|
|
for i in u64(0) .. end {
|
|
|
|
assert keys[i] == i
|
|
|
|
}
|
|
|
|
for i in u64(0) .. end {
|
|
|
|
m.delete(i)
|
|
|
|
assert m[i] == 0
|
|
|
|
}
|
|
|
|
assert m.len == 0
|
|
|
|
}
|
2021-06-20 07:09:24 +02:00
|
|
|
|
|
|
|
fn test_map_set_fixed_array_variable() {
|
|
|
|
mut m := map[string][2]f64{}
|
|
|
|
m['A'] = [1.1, 2.2]!
|
|
|
|
println(m)
|
|
|
|
assert '$m' == "{'A': [1.1, 2.2]}"
|
|
|
|
|
|
|
|
mut m2 := map[string][2]f64{}
|
|
|
|
arr := [1.1, 2.2]!
|
|
|
|
m2['A'] = arr
|
|
|
|
println(m2)
|
|
|
|
assert '$m2' == "{'A': [1.1, 2.2]}"
|
|
|
|
}
|