Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1ff04d5efbfa48974d1ffd3d33e0d5d07ccfc65b
[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 main(int argc, char** argv) {
92   s_mc_expression_state_t state;
93   memset(&state, 0, sizeof(s_mc_expression_state_t));
94
95   basic_test(&state);
96
97   for(int i=0; i!=100; ++i) {
98     uintptr_t a = rand();
99     uintptr_t b = rand();
100     assert(eval_binary_operation(&state, DW_OP_plus, a, b) == (a + b));
101   }
102
103   for(int i=0; i!=100; ++i) {
104     uintptr_t a = rand();
105     uintptr_t b = rand();
106     assert(eval_binary_operation(&state, DW_OP_or, a, b) == (a | b));
107   }
108
109   for(int i=0; i!=100; ++i) {
110     uintptr_t a = rand();
111     uintptr_t b = rand();
112     assert(eval_binary_operation(&state, DW_OP_and, a, b) == (a & b));
113   }
114
115   for(int i=0; i!=100; ++i) {
116     uintptr_t a = rand();
117     uintptr_t b = rand();
118     assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));
119   }
120
121   return 0;
122 }