Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7def465f70e602309720e7d0657a9495c1675c17
[simgrid.git] / testsuite / mc / dwarf_expression.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <string.h>
6 #include <assert.h>
7 #include <stdlib.h>
8
9 #include "../src/mc/mc_private.h"
10
11 static
12 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
13   state->stack_size = 0;
14
15   Dwarf_Op ops[15];
16   ops[0].atom = DW_OP_const8u;
17   ops[0].number = a;
18   ops[1].atom = DW_OP_const8u;
19   ops[1].number = b;
20   ops[2].atom = op;
21
22   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
23   assert(state->stack_size==1);
24   return state->stack[state->stack_size - 1];
25 }
26
27 static
28 void basic_test(mc_expression_state_t state) {
29   Dwarf_Op ops[60];
30
31   uintptr_t a = rand();
32   uintptr_t b = rand();
33
34   ops[0].atom = DW_OP_drop;
35   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
36
37   ops[0].atom = DW_OP_lit21;
38   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
39   assert(state->stack_size==1);
40   assert(state->stack[state->stack_size-1]==21);
41
42   ops[0].atom = DW_OP_const8u;
43   ops[0].number = a;
44   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
45   assert(state->stack_size==2);
46   assert(state->stack[state->stack_size-1] == a);
47
48   ops[0].atom = DW_OP_drop;
49   ops[1].atom = DW_OP_drop;
50   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
51   assert(state->stack_size==0);
52
53   ops[0].atom = DW_OP_lit21;
54   ops[1].atom = DW_OP_plus_uconst;
55   ops[1].number = a;
56   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
57   assert(state->stack_size==1);
58   assert(state->stack[state->stack_size-1]== a + 21);
59
60   state->stack_size = 0;
61   ops[0].atom = DW_OP_const8u;
62   ops[0].number = a;
63   ops[1].atom = DW_OP_dup;
64   ops[2].atom = DW_OP_plus;
65   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
66   assert(state->stack_size==1);
67   assert(state->stack[state->stack_size-1]== a + a);
68
69   state->stack_size = 0;
70   ops[0].atom = DW_OP_const8u;
71   ops[0].number = a;
72   ops[1].atom = DW_OP_const8u;
73   ops[1].number = b;
74   ops[2].atom = DW_OP_over;
75   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
76   assert(state->stack_size==3);
77   assert(state->stack[state->stack_size-1]== a);
78   assert(state->stack[state->stack_size-2]== b);
79   assert(state->stack[state->stack_size-3]== a);
80
81   state->stack_size = 0;
82   ops[0].atom = DW_OP_const8u;
83   ops[0].number = a;
84   ops[1].atom = DW_OP_const8u;
85   ops[1].number = b;
86   ops[2].atom = DW_OP_swap;
87   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
88   assert(state->stack_size=2);
89   assert(state->stack[state->stack_size-1]== a);
90   assert(state->stack[state->stack_size-2]== b);
91 }
92
93 static
94 void test_deref(mc_expression_state_t state) {
95   uintptr_t foo = 42;
96
97   Dwarf_Op ops[60];
98   ops[0].atom = DW_OP_const8u;
99   ops[0].number = (Dwarf_Word) &foo;
100   ops[1].atom = DW_OP_deref;
101   state->stack_size = 0;
102
103   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
104   assert(state->stack_size==1);
105   assert(state->stack[state->stack_size-1] == foo);
106 }
107
108 int main(int argc, char** argv) {
109   s_mc_expression_state_t state;
110   memset(&state, 0, sizeof(s_mc_expression_state_t));
111
112   basic_test(&state);
113
114   for(int i=0; i!=100; ++i) {
115     uintptr_t a = rand();
116     uintptr_t b = rand();
117     assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
118   }
119
120   for(int i=0; i!=100; ++i) {
121     uintptr_t a = rand();
122     uintptr_t b = rand();
123     assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
124   }
125
126   for(int i=0; i!=100; ++i) {
127     uintptr_t a = rand();
128     uintptr_t b = rand();
129     assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
130   }
131
132   for(int i=0; i!=100; ++i) {
133     uintptr_t a = rand();
134     uintptr_t b = rand();
135     assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));
136   }
137
138   test_deref(&state);
139
140   return 0;
141 }