mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
map_test.v: format + ref test
This commit is contained in:
parent
af5b2e1b3c
commit
961e778ed1
@ -522,7 +522,7 @@ fn (p &Parser) find_type(name string) &Type {
|
|||||||
return typ
|
return typ
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (t &Table) find_type(name_ string) *Type {
|
fn (t &Table) find_type(name_ string) &Type {
|
||||||
mut name := name_
|
mut name := name_
|
||||||
if name.ends_with('*') && !name.contains(' ') {
|
if name.ends_with('*') && !name.contains(' ') {
|
||||||
name = name.left(name.len - 1)
|
name = name.left(name.len - 1)
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
struct User {
|
struct User {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
struct A {
|
struct A {
|
||||||
m map[string]int
|
m map[string]int
|
||||||
users map[string]User
|
users map[string]User
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (a mut A) set(key string, val int) {
|
fn (a mut A) set(key string, val int) {
|
||||||
@ -15,45 +15,45 @@ fn (a mut A) set(key string, val int) {
|
|||||||
|
|
||||||
fn test_map() {
|
fn test_map() {
|
||||||
mut m := map[string]int
|
mut m := map[string]int
|
||||||
assert m.size == 0
|
assert m.size == 0
|
||||||
m['hi'] = 80
|
m['hi'] = 80
|
||||||
m['hello'] = 101
|
m['hello'] = 101
|
||||||
assert m['hi'] == 80
|
assert m['hi'] == 80
|
||||||
assert m['hello'] == 101
|
assert m['hello'] == 101
|
||||||
assert m.size == 2
|
assert m.size == 2
|
||||||
assert 'hi' in m
|
assert 'hi' in m
|
||||||
mut sum := 0
|
mut sum := 0
|
||||||
mut key_sum := ''
|
mut key_sum := ''
|
||||||
// Test `for in`
|
// Test `for in`
|
||||||
for key, val in m {
|
for key, val in m {
|
||||||
sum += val
|
sum += val
|
||||||
key_sum += key
|
key_sum += key
|
||||||
}
|
}
|
||||||
assert sum == 80 + 101
|
assert sum == 80 + 101
|
||||||
assert key_sum == 'hihello'
|
assert key_sum == 'hihello'
|
||||||
// Test `.keys()`
|
// Test `.keys()`
|
||||||
keys := m.keys()
|
keys := m.keys()
|
||||||
assert keys.len == 2
|
assert keys.len == 2
|
||||||
assert keys[0] == 'hi'
|
assert keys[0] == 'hi'
|
||||||
assert keys[1] == 'hello'
|
assert keys[1] == 'hello'
|
||||||
m.delete('hi')
|
m.delete('hi')
|
||||||
assert m.size == 1
|
assert m.size == 1
|
||||||
|
|
||||||
assert m['hi'] == 0
|
assert m['hi'] == 0
|
||||||
assert m.keys().len == 1
|
assert m.keys().len == 1
|
||||||
assert m.keys()[0] == 'hello'
|
assert m.keys()[0] == 'hello'
|
||||||
////
|
////
|
||||||
mut users := map[string]User
|
mut users := map[string]User
|
||||||
users['1'] = User{'Peter'}
|
users['1'] = User{'Peter'}
|
||||||
peter := users['1']
|
peter := users['1']
|
||||||
assert peter.name == 'Peter'
|
assert peter.name == 'Peter'
|
||||||
mut a := A{
|
mut a := A{
|
||||||
m: map[string]int
|
m: map[string]int
|
||||||
users: map[string]User
|
users: map[string]User
|
||||||
}
|
}
|
||||||
a.users['Bob'] = User{'Bob'}
|
a.users['Bob'] = User{'Bob'}
|
||||||
q := a.users['Bob']
|
q := a.users['Bob']
|
||||||
assert q.name == 'Bob'
|
assert q.name == 'Bob'
|
||||||
a.m['one'] = 1
|
a.m['one'] = 1
|
||||||
a.set('two', 2)
|
a.set('two', 2)
|
||||||
assert a.m['one'] == 1
|
assert a.m['one'] == 1
|
||||||
@ -61,29 +61,29 @@ fn test_map() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_map_init() {
|
fn test_map_init() {
|
||||||
m := { 'one': 1, 'two': 2 }
|
m := { 'one': 1, 'two': 2 }
|
||||||
assert m['one'] == 1
|
assert m['one'] == 1
|
||||||
assert m['two'] == 2
|
assert m['two'] == 2
|
||||||
assert m['three'] == 0
|
assert m['three'] == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_string_map() {
|
fn test_string_map() {
|
||||||
//m := map[string]Fn
|
//m := map[string]Fn
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_large_map() {
|
fn test_large_map() {
|
||||||
//ticks := time.ticks()
|
//ticks := time.ticks()
|
||||||
mut nums := map[string]int
|
mut nums := map[string]int
|
||||||
N := 30 * 1000
|
N := 30 * 1000
|
||||||
for i := 0; i < N; i++ {
|
for i := 0; i < N; i++ {
|
||||||
key := i.str()
|
key := i.str()
|
||||||
nums[key] = i
|
nums[key] = i
|
||||||
}
|
}
|
||||||
assert nums['1'] == 1
|
assert nums['1'] == 1
|
||||||
assert nums['999'] == 999
|
assert nums['999'] == 999
|
||||||
assert nums['1000000'] == 0
|
assert nums['1000000'] == 0
|
||||||
//println(time.ticks() - ticks)
|
//println(time.ticks() - ticks)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_various_map_value() {
|
fn test_various_map_value() {
|
||||||
mut m1 := map[string]int
|
mut m1 := map[string]int
|
||||||
@ -157,9 +157,20 @@ fn test_various_map_value() {
|
|||||||
|
|
||||||
|
|
||||||
fn test_string_arr() {
|
fn test_string_arr() {
|
||||||
mut m := map[string][]string
|
mut m := map[string][]string
|
||||||
m['a'] = ['one', 'two']
|
m['a'] = ['one', 'two']
|
||||||
assert m['a'].len == 2
|
assert m['a'].len == 2
|
||||||
assert m['a'][0] == 'one'
|
assert m['a'][0] == 'one'
|
||||||
assert m['a'][1] == 'two'
|
assert m['a'][1] == 'two'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn test_ref() {
|
||||||
|
m := { 'one': 1 }
|
||||||
|
// TODO "cannot take the address of m['one']"
|
||||||
|
mut one := &m['one']
|
||||||
|
one++
|
||||||
|
println(*one)
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user