Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rewrite of the fat trees implementation
[simgrid.git] / testsuite / mc / dwarf_expression.c
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifdef NDEBUG
8 #undef NDEBUG
9 #endif
10
11 #include <string.h>
12 #include <assert.h>
13 #include <stdlib.h>
14
15 #include "../src/mc/mc_private.h"
16
17 static
18 uintptr_t eval_binary_operation(mc_expression_state_t state, int op, uintptr_t a, uintptr_t b) {
19   state->stack_size = 0;
20
21   Dwarf_Op ops[15];
22   ops[0].atom = DW_OP_const8u;
23   ops[0].number = a;
24   ops[1].atom = DW_OP_const8u;
25   ops[1].number = b;
26   ops[2].atom = op;
27
28   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
29   assert(state->stack_size==1);
30   return state->stack[state->stack_size - 1];
31 }
32
33 static
34 void basic_test(mc_expression_state_t state) {
35   Dwarf_Op ops[60];
36
37   uintptr_t a = rand();
38   uintptr_t b = rand();
39
40   ops[0].atom = DW_OP_drop;
41   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_E_STACK_UNDERFLOW);
42
43   ops[0].atom = DW_OP_lit21;
44   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
45   assert(state->stack_size==1);
46   assert(state->stack[state->stack_size-1]==21);
47
48   ops[0].atom = DW_OP_const8u;
49   ops[0].number = a;
50   assert(mc_dwarf_execute_expression(1, ops, state) == MC_EXPRESSION_OK);
51   assert(state->stack_size==2);
52   assert(state->stack[state->stack_size-1] == a);
53
54   ops[0].atom = DW_OP_drop;
55   ops[1].atom = DW_OP_drop;
56   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
57   assert(state->stack_size==0);
58
59   ops[0].atom = DW_OP_lit21;
60   ops[1].atom = DW_OP_plus_uconst;
61   ops[1].number = a;
62   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
63   assert(state->stack_size==1);
64   assert(state->stack[state->stack_size-1]== a + 21);
65
66   state->stack_size = 0;
67   ops[0].atom = DW_OP_const8u;
68   ops[0].number = a;
69   ops[1].atom = DW_OP_dup;
70   ops[2].atom = DW_OP_plus;
71   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
72   assert(state->stack_size==1);
73   assert(state->stack[state->stack_size-1]== a + a);
74
75   state->stack_size = 0;
76   ops[0].atom = DW_OP_const8u;
77   ops[0].number = a;
78   ops[1].atom = DW_OP_const8u;
79   ops[1].number = b;
80   ops[2].atom = DW_OP_over;
81   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
82   assert(state->stack_size==3);
83   assert(state->stack[state->stack_size-1]== a);
84   assert(state->stack[state->stack_size-2]== b);
85   assert(state->stack[state->stack_size-3]== a);
86
87   state->stack_size = 0;
88   ops[0].atom = DW_OP_const8u;
89   ops[0].number = a;
90   ops[1].atom = DW_OP_const8u;
91   ops[1].number = b;
92   ops[2].atom = DW_OP_swap;
93   assert(mc_dwarf_execute_expression(3, ops, state) == MC_EXPRESSION_OK);
94   assert(state->stack_size=2);
95   assert(state->stack[state->stack_size-1]== a);
96   assert(state->stack[state->stack_size-2]== b);
97 }
98
99 static
100 void test_deref(mc_expression_state_t state) {
101   uintptr_t foo = 42;
102
103   Dwarf_Op ops[60];
104   ops[0].atom = DW_OP_const8u;
105   ops[0].number = (uintptr_t) &foo;
106   ops[1].atom = DW_OP_deref;
107   state->stack_size = 0;
108
109   assert(mc_dwarf_execute_expression(2, ops, state) == MC_EXPRESSION_OK);
110   assert(state->stack_size==1);
111   assert(state->stack[state->stack_size-1] == foo);
112 }
113
114 int main(int argc, char** argv) {
115   s_mc_expression_state_t state;
116   memset(&state, 0, sizeof(s_mc_expression_state_t));
117
118   basic_test(&state);
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_plus, 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_or, 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_and, a, b) == (a & b));
136   }
137
138   for(int i=0; i!=100; ++i) {
139     uintptr_t a = rand();
140     uintptr_t b = rand();
141     assert(eval_binary_operation(&state, DW_OP_xor, a, b) == (a ^ b));
142   }
143
144   test_deref(&state);
145
146   return 0;
147 }