-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestqueue.d
135 lines (120 loc) · 2.54 KB
/
testqueue.d
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
131
132
133
134
135
import esdl;
import std.stdio;
class queueObj
{
enum OP: ubyte {
REMOVE_FRONT_ONE,
REMOVE_BACK_ONE,
REMOVE_ONE,
REMOVE_FRONT,
REMOVE_BACK,
REMOVE,
INSERT,
PUSH_BACK,
PUSH_FRONT,
INSERT_ONE,
PUSH_BACK_ONE,
PUSH_FRONT_ONE,
}
mixin randomization;
Queue!ubyte _queue;
@rand ubyte[] _arr;
@rand uint _len;
@rand ubyte _elem;
@rand uint _pos;
uint _lenMax = 10;
this (uint maxLen) {
_lenMax = maxLen;
}
constraint!q{
_len <= _lenMax;
_len > 0;
_arr.length == _len;
} cst_len;
@rand OP _op;
constraint!q{
if (_queue.length == 0) {
_op >= OP.INSERT;
}
if (_queue.length > _lenMax) {
_op <= OP.REMOVE;
}
_pos <= _queue.length;
if (_op <= OP.REMOVE) {
_pos + _len <= _queue.length;
}
} cst_q;
Queue!ubyte _start;
void check(ubyte[] exp) {
if (_queue != exp) {
import std.stdio;
writefln("%s : %s : %s : %s : %s \n %s !=\n %s", _start, _op, _pos, _len, _arr,
exp, _queue);
_start.print();
_queue.print();
assert(false);
}
}
void post_randomize() {
ubyte[] arr = _queue.toArray();
_start = _queue.clone();
final switch (_op) {
case OP.REMOVE_FRONT_ONE:
_queue.popFront();
check(arr[1..$]);
break;
case OP.REMOVE_BACK_ONE:
_queue.popBack();
check(arr[0..$-1]);
break;
case OP.REMOVE_ONE:
_queue.remove(_pos, 1);
check(arr[0.._pos] ~ arr[_pos+1..$]);
break;
case OP.REMOVE_FRONT:
_queue.removeFront(_len);
check(arr[_len..$]);
break;
case OP.REMOVE_BACK:
_queue.removeBack(_len);
check(arr[0..$-_len]);
break;
case OP.REMOVE:
_queue.remove(_pos, _len);
check(arr[0.._pos] ~ arr[_pos+_len..$]);
break;
case OP.INSERT:
_queue.insert(_pos, _arr);
check(arr[0.._pos] ~ _arr ~ arr[_pos..$]);
break;
case OP.PUSH_BACK:
_queue.pushBack(_arr);
check(arr ~ _arr);
break;
case OP.PUSH_FRONT:
_queue.pushFront(_arr);
check(_arr ~ arr);
break;
case OP.INSERT_ONE:
_queue.insert(_pos, _elem);
check(arr[0.._pos] ~ _elem ~ arr[_pos..$]);
break;
case OP.PUSH_BACK_ONE:
_queue.pushBack(_elem);
check(arr ~ _elem);
break;
case OP.PUSH_FRONT_ONE:
_queue.pushFront(_elem);
check([_elem] ~ arr);
break;
}
}
}
void main()
{
queueObj test = new queueObj(10);
for (size_t i=0; i!=1000; ++i) {
test.randomize();
}
test._queue.print();
}