Logo AND Algorithmique Numérique Distribuée

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