2019-09-23 12:42:20 +02:00
|
|
|
// import time
|
2019-08-31 15:24:37 +02:00
|
|
|
|
2019-06-30 22:44:15 +02:00
|
|
|
struct User {
|
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:
|
2019-08-31 15:24:37 +02:00
|
|
|
m map[string]int
|
|
|
|
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() {
|
2019-08-16 17:44:35 +02: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-31 10:46:00 +02:00
|
|
|
|
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'
|
|
|
|
////
|
|
|
|
mut users := map[string]User
|
|
|
|
users['1'] = User{'Peter'}
|
2019-06-30 22:44:15 +02:00
|
|
|
peter := users['1']
|
2019-08-31 15:24:37 +02:00
|
|
|
assert peter.name == 'Peter'
|
2020-05-27 18:12:34 +02:00
|
|
|
mut a := Aaa{
|
2019-08-31 15:24:37 +02: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'
|
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() {
|
2019-08-31 15:24:37 +02:00
|
|
|
m := { 'one': 1, 'two': 2 }
|
|
|
|
assert m['one'] == 1
|
|
|
|
assert m['two'] == 2
|
|
|
|
assert m['three'] == 0
|
|
|
|
}
|
2019-08-03 09:44:08 +02:00
|
|
|
|
2019-07-14 11:01:32 +02:00
|
|
|
fn test_string_map() {
|
|
|
|
//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() {
|
|
|
|
//ticks := time.ticks()
|
|
|
|
mut nums := map[string]int
|
2020-04-02 23:42:08 +02:00
|
|
|
n := 30 * 1000
|
|
|
|
for i in 0..n {
|
2019-07-14 11:01:32 +02:00
|
|
|
key := i.str()
|
|
|
|
nums[key] = i
|
|
|
|
}
|
2019-08-31 15:24:37 +02:00
|
|
|
assert nums['1'] == 1
|
|
|
|
assert nums['999'] == 999
|
|
|
|
assert nums['1000000'] == 0
|
|
|
|
//println(time.ticks() - ticks)
|
|
|
|
}
|
2019-07-15 12:33:18 +02:00
|
|
|
|
|
|
|
fn test_various_map_value() {
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m1 := map[string]int
|
2019-07-15 12:33:18 +02:00
|
|
|
m1['test'] = 1
|
|
|
|
assert m1['test'] == 1
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m2 := map[string]string
|
2019-07-15 12:33:18 +02:00
|
|
|
m2['test'] = 'test'
|
|
|
|
assert m2['test'] == 'test'
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m3 := map[string]i8
|
2019-07-15 12:33:18 +02:00
|
|
|
m3['test'] = i8(0)
|
|
|
|
assert m3['test'] == i8(0)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m4 := map[string]i16
|
2019-07-15 12:33:18 +02:00
|
|
|
m4['test'] = i16(0)
|
|
|
|
assert m4['test'] == i16(0)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m7 := map[string]u16
|
2019-07-15 12:33:18 +02:00
|
|
|
m7['test'] = u16(0)
|
|
|
|
assert m7['test'] == u16(0)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m8 := map[string]u32
|
2019-07-15 12:33:18 +02:00
|
|
|
m8['test'] = u32(0)
|
|
|
|
assert m8['test'] == u32(0)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m9 := map[string]bool
|
2019-07-15 12:33:18 +02:00
|
|
|
m9['test'] = true
|
|
|
|
assert m9['test'] == true
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m10 := map[string]byte
|
2019-07-15 12:33:18 +02:00
|
|
|
m10['test'] = byte(0)
|
|
|
|
assert m10['test'] == byte(0)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02: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)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-16 17:44:35 +02: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-07-01 00:53:53 +02:00
|
|
|
//mut m13 := map[string]rune
|
|
|
|
//m13['test'] = rune(0)
|
|
|
|
//assert m13['test'] == rune(0)
|
2019-07-15 12:33:18 +02:00
|
|
|
|
2019-12-21 23:44:16 +01:00
|
|
|
mut m14 := map[string]voidptr
|
|
|
|
m14['test'] = voidptr(0)
|
|
|
|
assert m14['test'] == voidptr(0)
|
2019-07-15 12:33:18 +02:00
|
|
|
|
2019-12-21 23:44:16 +01:00
|
|
|
mut m15 := map[string]byteptr
|
|
|
|
m15['test'] = byteptr(0)
|
|
|
|
assert m15['test'] == byteptr(0)
|
2019-07-18 04:32:49 +02:00
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m16 := map[string]i64
|
2019-07-18 04:32:49 +02:00
|
|
|
m16['test'] = i64(0)
|
|
|
|
assert m16['test'] == i64(0)
|
|
|
|
|
2019-08-16 17:44:35 +02:00
|
|
|
mut m17 := map[string]u64
|
2019-07-18 04:32:49 +02:00
|
|
|
m17['test'] = u64(0)
|
|
|
|
assert m17['test'] == u64(0)
|
2019-12-21 23:44:16 +01:00
|
|
|
|
|
|
|
mut m18 := map[string]&int
|
|
|
|
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() {
|
2019-08-31 15:24:37 +02:00
|
|
|
mut m := map[string][]string
|
|
|
|
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() {
|
|
|
|
mut m := map[string]int
|
|
|
|
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() {
|
|
|
|
mut m := map[string]int
|
|
|
|
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-08-31 15:24:37 +02:00
|
|
|
/*
|
|
|
|
fn test_ref() {
|
|
|
|
m := { 'one': 1 }
|
|
|
|
// TODO "cannot take the address of m['one']"
|
|
|
|
mut one := &m['one']
|
|
|
|
one++
|
|
|
|
println(*one)
|
2019-12-19 19:17:12 +01:00
|
|
|
|
2019-08-31 15:24:37 +02:00
|
|
|
}
|
|
|
|
*/
|
2019-12-19 19:17:12 +01:00
|
|
|
|
|
|
|
fn test_delete_size() {
|
|
|
|
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
|
|
|
|
}
|
2020-06-21 16:51:02 +02:00
|
|
|
assert(m.len == 10)
|
|
|
|
println(m.len)
|
2019-12-19 19:17:12 +01:00
|
|
|
for i in 0..10 {
|
|
|
|
m.delete(arr[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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{}
|
|
|
|
a = {
|
|
|
|
'x': 12.4
|
|
|
|
'y': 3
|
|
|
|
}
|
|
|
|
b = {
|
|
|
|
'u': -13
|
|
|
|
'v': 12
|
|
|
|
}
|
|
|
|
c = {
|
|
|
|
's': u16(5)
|
|
|
|
't': 3
|
|
|
|
}
|
2020-06-24 20:41:26 +02:00
|
|
|
_ := Mstruct1 {
|
2020-06-19 15:00:27 +02:00
|
|
|
{ 'p': 12 }
|
|
|
|
}
|
2020-06-24 20:41:26 +02:00
|
|
|
_ := Mstruct2 {
|
2020-06-19 15:00:27 +02:00
|
|
|
{ 'q': 1.7 }
|
|
|
|
}
|
2020-06-24 20:41:26 +02:00
|
|
|
_ := Mstruct3 {
|
2020-06-19 15:00:27 +02:00
|
|
|
{ 'r': u16(6), 's': 5 }
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 20:41:26 +02:00
|
|
|
|
|
|
|
fn test_postfix_op_directly() {
|
|
|
|
mut a := map[string]int
|
|
|
|
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() {
|
|
|
|
mut a := map[string][]string
|
|
|
|
a['aaa'] << ['a', 'b', 'c']
|
|
|
|
assert a['aaa'].len == 3
|
|
|
|
assert a['aaa'] == ['a', 'b', 'c']
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_assign_directly() {
|
|
|
|
mut a := map[string]int
|
|
|
|
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() {
|
|
|
|
for k, v in {'aa': 1} {
|
|
|
|
assert k == 'aa'
|
|
|
|
assert v == 1
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 13:34:59 +02:00
|
|
|
|
|
|
|
fn test_plus_assign_string() {
|
|
|
|
mut m := {'one': ''}
|
|
|
|
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() {
|
|
|
|
m := {'a': 'b', 'c': 'd'}
|
|
|
|
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() {
|
|
|
|
mut m := {'one': 1}
|
|
|
|
map_in_mut(mut m)
|
|
|
|
assert m['one'] == 2
|
|
|
|
}
|