-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer-chart.c
146 lines (125 loc) · 3.37 KB
/
timer-chart.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* test.c
*
* program to test seconds calculations to verify correctness.
*
* Prints out a table of calculated vs expected values. table will include an "X" in each cell that has an error.
*
* If there are errors, print out the calculations that have errors.
*
*
* Created on: 31 Aug 2021
* Author: claude
*/
#include <stdarg.h>
#include <stdio.h>
static int g_full_time;
static int g_offset_time;
char* lbls[] = { "f-o-", "f-o+", "f+o-", "f+o+" };
static int seconds( int ticks ) {
int result;
if (g_offset_time) {
if (g_full_time) { // f+o+
result = (ticks+500)/1000;
} else { // f-o+
int sec = (ticks)/1000;
result = sec+((sec+1)%2);
}
} else {
if (g_full_time) { // f+o-
result = ticks/1000;
} else { // f-o-
result = 1+( ((ticks-1000)/2000)* 2);
}
}
return result;
}
static int assert( int x, char* stmt) {
if (!x) {
printf( "%s\n", stmt );
return 1;
}
return 0;
}
int main(int argc, char **argv, char **envp)
{
/* A list of seconds and expected values for each calculation type.
* Seconds were chosen based on transition points in the calculations.
*/
// sec, f-o- f-o+ f+o- f+o+
int tests[][5] = {
{400, 1, 1, 0, 0},
{499, 1, 1, 0, 0},
{500, 1, 1, 0, 1},
{999, 1, 1, 0, 1},
{1000, 1, 1, 1, 1},
{1499, 1, 1, 1, 1},
{1500, 1, 1, 1, 2},
{1999, 1, 1, 1, 2},
{2000, 1, 3, 2, 2},
{2499, 1, 3, 2, 2},
{2500, 1, 3, 2, 3},
{2999, 1, 3, 2, 3},
{3000, 3, 3, 3, 3},
{3499, 3, 3, 3, 3},
{3500, 3, 3, 3, 4},
{3999, 3, 3, 3, 4},
{4000, 3, 5, 4, 4},
{4499, 3, 5, 4, 4},
{4500, 3, 5, 4, 5},
{4999, 3, 5, 4, 5},
{5000, 5, 5, 5, 5},
{5499, 5, 5, 5, 5},
{5500, 5, 5, 5, 6},
{5999, 5, 5, 5, 6},
{6000, 5, 7, 6, 6},
{6499, 5, 7, 6, 6},
{6500, 5, 7, 6, 7},
{6999, 5, 7, 6, 7},
{7000, 7, 7, 7, 7},
{7499, 7, 7, 7, 7},
{7500, 7, 7, 7, 8},
{7999, 7, 7, 7, 8},
{8000, 7, 9, 8, 8}
};
int test_count = sizeof(tests)/sizeof(tests[0]);
printf( "timer-chart -- displays the number of seconds reported based on the f and o properties.\n" );
printf( "Prints out a table of calculated vs expected values. table will include an 'X' in each cell that has an error.\n\n");
printf( "t = timer ticks (milliseconds)\n");
printf( "f-/f+ = --full-time not specified/specified\n" );
printf( "o-/o+ = --offset-time not specified/specified\n\n" );
printf( " t | %s | %s | %s | %s \n", lbls[0], lbls[1], lbls[2], lbls[3] );
int err = 0;
for (int i=0;i<test_count;i++)
{
int t = tests[i][0];
printf( "%4d |", t );
for (int l=0;l<4;l++)
{
g_full_time = l & 0x2;
g_offset_time = l & 0x1;
int result = seconds(t);
int expected = tests[i][1+l];
err |= result!=tests[i][1+l];
printf( " %i %c %i |", result, result==expected?' ':'X', expected );
}
printf( "\n" );
}
// If there was an error print out detail.
if (err) {
char sout[250];
for (int i=0;i<test_count;i++)
{
for (int l=0;l<4;l++)
{
g_full_time = l & 0x2;
g_offset_time = l & 0x1;
int t = tests[i][0];
int expected = tests[i][1+l];
int result = seconds(t);
sprintf( sout, "%s %i yields %i not %i (%i %i)", lbls[l], t, result, expected, g_full_time, g_offset_time );
assert( result==expected, sout );
}
}
}
}