-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_test.v
130 lines (99 loc) · 2.23 KB
/
dict_test.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
module py
fn testsuite_begin() {
C.Py_Initialize()
}
fn testsuite_end() {
}
fn test_dict_check() {
d := C.PyDict_New()
assert C.PyDict_Check(d) == 1
assert C.PyDict_CheckExact(d) == 1
C.Py_XDECREF(d)
}
fn test_dict_get_set() {
d := C.PyDict_New()
defer {
C.Py_DECREF(d)
}
vd := {
'key1': 1,
'key2': 2
}
for key,val in vd {
k := C.PyUnicode_FromString(key.str)
v := C.PyLong_FromLong(val)
assert k.ptr() != C.NULL
assert v.ptr() != C.NULL
defer {
C.Py_DECREF(k)
C.Py_DECREF(v)
}
err := C.PyDict_SetItem(d, k, v)
assert err == 0
}
for key,val in vd {
k := C.PyUnicode_FromString(key.str)
v := C.PyLong_FromLong(val)
defer {
C.Py_DECREF(k)
C.Py_DECREF(v)
}
r := C.PyDict_GetItem(d, k)
assert C.PyLong_Check(r) == 1
vv := C.PyLong_AsLong(r)
assert vv == val
}
k := C.PyUnicode_FromString(c'not-in-dict')
should_be_null := C.PyDict_GetItem(d, k)
assert should_be_null.ptr() == C.NULL
C.Py_DECREF(k)
}
fn test_dict_get_string() {
d := C.PyDict_New()
k := C.PyUnicode_FromString(c'key')
v := C.PyLong_FromLong(42)
assert k.ptr() != C.NULL
assert v.ptr() != C.NULL
defer {
C.Py_DECREF(k)
C.Py_DECREF(v)
}
err := C.PyDict_SetItem(d, k, v)
assert err == 0
r := C.PyDict_GetItemString(d, c'key')
assert C.PyLong_Check(r) == 1
vv := C.PyLong_AsLong(r)
assert vv == 42
C.Py_DECREF(d)
}
fn test_dict_set_default() {
$if !py_limited_api ? {
d := C.PyDict_New()
k := C.PyUnicode_FromString(c'key')
v := C.PyLong_FromLong(42)
default_obj := C.PyLong_FromLong(24)
assert k.ptr() != C.NULL
assert v.ptr() != C.NULL
assert default_obj.ptr() != C.NULL
err := C.PyDict_SetItem(d, k, v)
assert err == 0
r := C.PyDict_SetDefault(d, k, default_obj)
assert r.ptr() != C.NULL
C.Py_INCREF(r)
C.Py_DECREF(default_obj)
C.PyLong_Check(r)
C.Py_DECREF(d)
C.Py_DECREF(k)
C.Py_DECREF(v)
}
}
fn test_dict_get_with_err() {
d := C.PyDict_New()
k := C.PyUnicode_FromString(c'not-in-dict')
r := C.PyDict_GetItemWithError(d, k)
assert r.ptr() == C.NULL
e := C.PyErr_Occurred()
assert e.ptr() == C.NULL
C.Py_DECREF(k)
C.Py_DECREF(d)
}