| 1 |
project('dict test', 'c') |
| 2 |
|
| 3 |
dict = {'foo' : 'bar', |
| 4 |
'baz' : 'foo', |
| 5 |
'foo bar': 'baz'} |
| 6 |
|
| 7 |
exe = executable('prog', sources : ['prog.c']) |
| 8 |
|
| 9 |
i = 0 |
| 10 |
|
| 11 |
foreach key, value : dict |
| 12 |
test('dict test @0@'.format(key), exe, |
| 13 |
args : [dict[key], value]) |
| 14 |
i += 1 |
| 15 |
endforeach |
| 16 |
|
| 17 |
assert(i == 3, 'There should be three elements in that dictionary') |
| 18 |
|
| 19 |
# Test keys() works as expected (note that it sorts the keys) |
| 20 |
assert(dict.keys() == ['baz', 'foo', 'foo bar'], 'Keys returned unexpected list') |
| 21 |
|
| 22 |
# Test values() works as expected (note that it sorts first, to match keys()) |
| 23 |
assert(dict.values() == ['foo', 'bar', 'baz']) |
| 24 |
|
| 25 |
empty_dict = {} |
| 26 |
|
| 27 |
foreach key, value : empty_dict |
| 28 |
assert(false, 'This dict should be empty') |
| 29 |
endforeach |
| 30 |
|
| 31 |
# Test keys() returns an empty list |
| 32 |
assert(empty_dict.keys() == [], 'Keys for an empty dict should be an empty list') |
| 33 |
|
| 34 |
# Test values() returns an empty list |
| 35 |
assert(empty_dict.values() == [], 'Values for an empty dict should be an empty list') |
| 36 |
|
| 37 |
d1 = empty_dict + {'a' : 'b'} |
| 38 |
assert(d1 == {'a' : 'b'}, 'dict addition is not working') |
| 39 |
|
| 40 |
d2 = d1 + {'a' : 'b2', 'c' : 'd'} |
| 41 |
assert(d2 == {'a' : 'b2', 'c' : 'd'}, 'dict addition is not working') |
| 42 |
assert(d1 == {'a' : 'b'}, 'dict should be immutable') |
| 43 |
|
| 44 |
d3 = d2 |
| 45 |
d3 += {'e' : 'f'} |
| 46 |
assert(d3 == {'a' : 'b2', 'c' : 'd', 'e' : 'f'}, 'dict plusassign is not working') |
| 47 |
assert(d2 == {'a' : 'b2', 'c' : 'd'}, 'dict should be immutable') |
| 48 |
|
| 49 |
dict1 = {} |
| 50 |
|
| 51 |
# A variable to be used as a key |
| 52 |
testkey1 = 'myKey1' |
| 53 |
testkey2 = 'myKey2' |
| 54 |
|
| 55 |
# Add new entry using the variable |
| 56 |
dict1 += {testkey1 : 'myValue'} |
| 57 |
dict1 += {testkey2 : 42} |
| 58 |
|
| 59 |
# Test that the stored values are correct |
| 60 |
assert(dict1[testkey1] == 'myValue', |
| 61 |
'Incorrect string value retrieved from dictionary - variable key') |
| 62 |
assert(dict1['myKey1'] == 'myValue', |
| 63 |
'Incorrect string value retrieved from dictionary - literal key') |
| 64 |
assert(dict1[testkey2] == 42, |
| 65 |
'Incorrect int value retrieved from dictionary - variable key') |
| 66 |
assert(dict1['myKey2'] == 42, |
| 67 |
'Incorrect int value retrieved from dictionary - literal key') |
| 68 |
|
| 69 |
d = {testkey1 : 1} |
| 70 |
assert(d[testkey1] == 1, |
| 71 |
'Incorrect int value retrieved from dictionary - variable key') |
| 72 |
assert(d['myKey1'] == 1, |
| 73 |
'Incorrect int value retrieved from dictionary - literal key') |
| 74 |
|
| 75 |
d = {'1' / '2' : 1, join_paths('a', 'b') : 2} |
| 76 |
k1 = '1' / '2' |
| 77 |
k2 = join_paths('a', 'b') |
| 78 |
assert(d[k1] == 1, 'Incorrect expression evaluation in dictionary key') |
| 79 |
assert(d[k2] == 2, 'Incorrect expression evaluation in dictionary key') |
| 80 |
|
| 81 |
d = {'a' + 'b' : 1} |
| 82 |
assert(d['a' + 'b'] == 1, 'Incorrect expression evaluation in dictionary key') |
| 83 |
assert(d['ab'] == 1, 'Incorrect expression evaluation in dictionary key') |
| 84 |
|
| 85 |
# Complex types |
| 86 |
d = { |
| 87 |
'sanity': 1, |
| 88 |
'host': host_machine, |
| 89 |
'meson': meson, |
| 90 |
} |
| 91 |
|
| 92 |
assert(d['sanity'] == 1) |
| 93 |
assert(not is_disabler(d['meson'])) |
| 94 |
assert(not is_disabler(d['host'])) |