Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix two teshes for out of source buiilds
[simgrid.git] / teshsuite / mc / dwarf-expression / dwarf-expression.cpp
1 /* Copyright (c) 2014-2015. 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 #include "src/mc/Process.hpp"
18 #include "src/mc/Type.hpp"
19 #include "src/mc/ObjectInformation.hpp"
20 #include "src/mc/Variable.hpp"
21
22 static simgrid::mc::Process* process;
23
24 static
25 uintptr_t eval_binary_operation(
26   simgrid::dwarf::ExpressionContext& state, int op, uintptr_t a, uintptr_t b) {
27
28   Dwarf_Op ops[15];
29   ops[0].atom = DW_OP_const8u;
30   ops[0].number = a;
31   ops[1].atom = DW_OP_const8u;
32   ops[1].number = b;
33   ops[2].atom = op;
34
35   simgrid::dwarf::ExpressionStack stack;
36
37   try {
38     simgrid::dwarf::execute(ops, 3, state, stack);
39   }
40   catch(std::runtime_error& e) {
41     fprintf(stderr,"Expression evaluation error");
42   }
43
44   assert(stack.size() == 1);
45   return stack.top();
46 }
47
48 static
49 void basic_test(simgrid::dwarf::ExpressionContext const& state) {
50   try {
51
52   Dwarf_Op ops[60];
53
54   uintptr_t a = rand();
55   uintptr_t b = rand();
56
57   simgrid::dwarf::ExpressionStack stack;
58
59   try {
60     ops[0].atom = DW_OP_drop;
61     simgrid::dwarf::execute(ops, 1, state, stack);
62     fprintf(stderr,"Exception expected");
63   }
64   catch(simgrid::dwarf::evaluation_error& e) {}
65
66   ops[0].atom = DW_OP_lit21;
67   simgrid::dwarf::execute(ops, 1, state, stack);
68   assert(stack.size() == 1);
69   assert(stack.top() == 21);
70
71   ops[0].atom = DW_OP_const8u;
72   ops[0].number = a;
73   simgrid::dwarf::execute(ops, 1, state, stack);
74   assert(stack.size() == 2);
75   assert(stack.top() == a);
76
77   ops[0].atom = DW_OP_drop;
78   ops[1].atom = DW_OP_drop;
79   simgrid::dwarf::execute(ops, 2, state, stack);
80   assert(stack.empty());
81
82   stack.clear();
83   ops[0].atom = DW_OP_lit21;
84   ops[1].atom = DW_OP_plus_uconst;
85   ops[1].number = a;
86   simgrid::dwarf::execute(ops, 2, state, stack);
87   assert(stack.size() == 1);
88   assert(stack.top() == a + 21);
89
90   stack.clear();
91   ops[0].atom = DW_OP_const8u;
92   ops[0].number = a;
93   ops[1].atom = DW_OP_dup;
94   ops[2].atom = DW_OP_plus;
95   simgrid::dwarf::execute(ops, 3, state, stack);
96   assert(stack.size() == 1);
97   assert(stack.top() == a + a);
98
99   stack.clear();
100   ops[0].atom = DW_OP_const8u;
101   ops[0].number = a;
102   ops[1].atom = DW_OP_const8u;
103   ops[1].number = b;
104   ops[2].atom = DW_OP_over;
105   simgrid::dwarf::execute(ops, 3, state, stack);
106   assert(stack.size() == 3);
107   assert(stack.top()  == a);
108   assert(stack.top(1) == b);
109   assert(stack.top(2) == a);
110
111   stack.clear();
112   ops[0].atom = DW_OP_const8u;
113   ops[0].number = a;
114   ops[1].atom = DW_OP_const8u;
115   ops[1].number = b;
116   ops[2].atom = DW_OP_swap;
117   simgrid::dwarf::execute(ops, 3, state, stack);
118   assert(stack.size() == 2);
119   assert(stack.top()  == a);
120   assert(stack.top(1) == b);
121
122   }
123   catch(std::runtime_error& e) {
124     fprintf(stderr,"Expression evaluation error");
125   }
126 }
127
128 static
129 void test_deref(simgrid::dwarf::ExpressionContext const& state) {
130   try {
131
132   uintptr_t foo = 42;
133
134   Dwarf_Op ops[60];
135   ops[0].atom = DW_OP_const8u;
136   ops[0].number = (uintptr_t) &foo;
137   ops[1].atom = DW_OP_deref;
138
139   simgrid::dwarf::ExpressionStack stack;
140
141   simgrid::dwarf::execute(ops, 2, state, stack);
142   assert(stack.size() == 1);
143   assert(stack.top()  == foo);
144
145   }
146   catch(std::runtime_error& e) {
147     fprintf(stderr,"Expression evaluation error");
148   }
149 }
150
151 int main(int argc, char** argv) {
152   process = new simgrid::mc::Process(getpid(), -1);
153   process->init();
154
155   simgrid::dwarf::ExpressionContext state;
156   state.address_space = (simgrid::mc::AddressSpace*) process;
157
158   basic_test(state);
159
160   for(int i=0; i!=100; ++i) {
161     uintptr_t a = rand();
162     uintptr_t b = rand();
163     assert(eval_binary_operation(state, DW_OP_plus, a, b) == (a + b));
164   }
165
166   for(int i=0; i!=100; ++i) {
167     uintptr_t a = rand();
168     uintptr_t b = rand();
169     assert(eval_binary_operation(state, DW_OP_or, a, b) == (a | b));
170   }
171
172   for(int i=0; i!=100; ++i) {
173     uintptr_t a = rand();
174     uintptr_t b = rand();
175     assert(eval_binary_operation(state, DW_OP_and, a, b) == (a & b));
176   }
177
178   for(int i=0; i!=100; ++i) {
179     uintptr_t a = rand();
180     uintptr_t b = rand();
181     assert(eval_binary_operation(state, DW_OP_xor, a, b) == (a ^ b));
182   }
183
184   test_deref(state);
185
186   return 0;
187 }