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
|
#include <stdlib.h>
#include <time.h>
#include <criterion/criterion.h>
extern int bouncingBall (double h, double bounce, double window);
static void testequal(double h, double bounce, double window, int expected) {
int actual = bouncingBall(h, bounce, window);
cr_assert_eq(actual, expected,
"for h = %f, bounce = %f, window = %f\n"
"expected %d, but got %d",
h, bounce, window,
expected, actual
);
}
static int solution (double h, double bounce, double window) {
if ((h <= 0) || (window >= h) || (bounce <= 0) || (bounce >= 1))
return -1;
int seen = -1;
while (h > window) {
seen += 2;
h = h * bounce;
}
return seen;
}
static int randomGen(int a, int b){
int r = rand();
return r % (b - a) + a;
}
Test(bouncingBall, randomTests) {
srand(time(NULL));
enum {N = 30};
const double someheights[N] = {
12, 10.5, 144, 233, 15.25, 61, 98, 15.9, 25.8, 41.8, 67, 109, 17, 28, 46,
7.5, 12.20, 19, 3, 5, 83, 13, 21, 35.5, 57, 92, 14, 24, 39, 6.5
};
const double someBounces[N] = {
0.6, 0.6, 0.6, 0.6, 0.6, 1.1, 9, 1, 0.6, 0.6, 0.6, 0.75, 0.75, 0.75, 0.75,
0.75, 12.20, 0.75, 0.75, 0.83, 0.13, 0.21, 0.35, 0.57, 0.9, 0.14, 0.24, 0.39, 0.65, 0.65
};
const double somewin[N] = {
1.5, 1.5, 1.44, 2.33, 1, 6.1, 9.8, 1.9, 2.8, 4.8, 3, 1.09, 1.7, 2.8, 46, 7.5,
12.20, 1.9, 3, 5, 0.83, 1.3, 2.1, 3.5, 0.57, 0.92, 1.4, 2.4, 3.9, 6.5
};
for (int k = 0; k < 100; k++) {
int i = randomGen(0, N - 1);
double h = someheights[i];
double b = someBounces[i];
double w = somewin[i];
testequal(h, b, w, solution(h, b, w));
}
}
Test(bouncingBall, sampleTests) {
testequal(2, 0.5, 1.0, 1);
testequal(3, 0.66, 1.5, 3);
testequal(30, 0.66, 1.5, 15);
testequal(30, 0.75, 1.5, 21);
testequal(30, 0.4, 10, 3);
testequal(40, 0.4, 10, 3);
testequal(10, 0.6, 10, -1);
testequal(40, 1, 10, -1);
testequal(-5, 0.66, 1.5, -1);
testequal(5, -1, 1.5, -1);
testequal(4, 0.25, 1.0, 1);
}
|