-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
100 lines (79 loc) · 2.11 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "klee/klee.h"
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#define THRESH 0.001
#define SMALLEST_ABS 0.01
/*
* KLEE defines a bunch of stuff as verification-only special functions that get
* dealt with inside the LLVM machinery. If we want to actually *run* the
* program with generated test cases, then we need to provide a runtime version
* of these things.
*/
#ifdef KLEE_RUNTIME
// A bit of a trick - if you're actually executing this function, you know that
// no values are actually symbolic (i.e. they've been substituted in by the
// test case runtime).
unsigned klee_is_symbolic(uintptr_t x) { return 0; }
bool klee_is_nan_float(float f) { return isnan(f); }
bool klee_is_infinite_float(float f) { return isinf(f); }
#else
unsigned klee_is_symbolic(uintptr_t x);
#endif
void sort(float* in, int n)
{
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (in[i] < in[j] && i < j) {
float tmp = in[i];
in[i] = in[j];
in[j] = tmp;
}
}
}
}
float mse1(float* inputs, int n)
{
float sum = 0.0;
for (int i = 0; i < n; i++) {
sum += inputs[i];
}
return sum;
}
float mse2(float* inputs, int n)
{
sort(inputs, n);
return mse1(inputs, n);
}
float fl_abs(float in)
{
if (in < 0.0) {
return -in;
} else {
return in;
}
}
int main()
{
int n = 2;
float inputs[n];
klee_make_symbolic(inputs, sizeof(inputs), "inputs");
for (int i = 0; i < n; i++) {
klee_assume(inputs[i] > SMALLEST_ABS);
// Uncomment the lines below to get rid of 'uninteresting' cases where your
// assertion might break down (e.g. infinite, NaN, really large values).
/* klee_assume(fl_abs(inputs[i]) < 1024); */
/* klee_assume(!klee_is_nan_float(inputs[i])); */
/* klee_assume(!klee_is_infinite_float(inputs[i])); */
}
float m1 = mse1(inputs, n);
float m2 = mse2(inputs, n);
if (!klee_is_symbolic(inputs[0])) {
for (int i = 0; i < n; ++i) {
printf("[%d] = %f\n", i, inputs[i]);
}
printf("%f : %f : %f\n", m1, m2, ((m1 - m2) / m1));
}
klee_assert(fl_abs((m1 - m2) / m1) < THRESH);
}