Debian Codegraph @

gcc/testsuite/gdc.test/runnable/testthread2.d

raw
1 // PERMUTE_ARGS:
2 // TRANSFORM_OUTPUT: remove_lines("warning: rand\(\) may return deterministic values")
3
4 // Quick, dirty and inefficient AA using linear search, useful for testing.
5 struct LinearAA(K, V) {
6 K[] keys;
7 V[] values;
8
9 V opIndex(K key) {
10 foreach(i, k; keys) {
11 if(k == key) {
12 return values[i];
13 }
14 }
15
16 assert(0, "Key not present.");
17 }
18
19 V opIndexAssign(V val, K key) {
20 foreach(i, k; keys) {
21 if(k == key) {
22 return values[i] = val;
23 }
24 }
25
26 keys ~= key;
27 values ~= val;
28 return val;
29 }
30
31 V* opBinaryRight(string op : "in")(K key) {
32 foreach(i, k; keys) {
33 if(key == k) {
34 return values.ptr + i;
35 }
36 }
37
38 return null;
39 }
40
41 void remove(K key) {
42 size_t i = 0;
43 for(; i < keys.length; i++) {
44 if(keys[i] == key) {
45 break;
46 }
47 }
48
49 assert(i < keys.length);
50
51 for(; i < keys.length - 1; i++) {
52 keys[i] = keys[i + 1];
53 values[i] = values[i + 1];
54 }
55
56 keys = keys[0..$ - 1];
57 values = values[0..$ - 1];
58 }
59
60 size_t length() {
61 return values.length;
62 }
63 }
64
65 extern (C) int rand();
66
67 uint random(const uint max = uint.max)
68 {
69 version (Windows)
70 {
71 // RAND_MAX is quite low for windows, extend the range by
72 // abusing multiple random values and rounding errors.
73 const a = rand(), b = rand();
74 const c = a < b ? double(a) / b : double(b) / a;
75 return (cast(int) (c * max)) % max;
76 }
77 else
78 return rand() % max;
79 }
80
81 void main()
82 {
83 foreach(iter; 0..10) { // Bug only happens after a few iterations.
84
85 uint[size_t] builtin;
86 LinearAA!(size_t, uint) linAA;
87 uint[] nums = new uint[100_000];
88 foreach(ref num; nums) {
89 num = random();
90 }
91
92 foreach(i; 0..10_000) {
93 auto index = random(cast(uint) nums.length);
94 if(index in builtin) {
95 assert(index in linAA);
96 assert(builtin[index] == nums[index]);
97 assert(linAA[index] == nums[index]);
98 builtin.remove(index);
99 linAA.remove(index);
100 } else {
101 assert(!(index in linAA));
102 builtin[index] = nums[index];
103 linAA[index] = nums[index];
104 }
105 }
106
107 assert(builtin.length == linAA.length);
108 foreach(k, v; builtin) {
109 assert(k in linAA);
110 assert(*(k in builtin) == *(k in linAA));
111 assert(linAA[k] == v);
112 }
113 }
114 }