Skip to content

Commit cf7919d

Browse files
committed
Copy tests from fps
1 parent bdce460 commit cf7919d

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

tests/test_aps.c

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2026 Mika Tuupola
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
-cut-
26+
27+
This file is part of the HAGL graphics library:
28+
https://github.com/tuupola/hagl
29+
30+
SPDX-License-Identifier: MIT
31+
32+
*/
33+
34+
#include <string.h>
35+
#include <time.h>
36+
37+
#include "greatest.h"
38+
39+
#include "aps.h"
40+
41+
static aps_instance_t aps;
42+
43+
static void setup_callback(void *data) {
44+
memset(&aps, 0, sizeof(aps));
45+
}
46+
47+
TEST test_aps_init_defaults(void) {
48+
aps_init(&aps);
49+
50+
ASSERT_EQ(0, aps.value);
51+
ASSERT_EQ(0.0, aps.current);
52+
ASSERT_IN_RANGE(0.98, aps.smoothing, 0.001);
53+
54+
PASS();
55+
}
56+
57+
TEST test_aps_init_custom_smoothing(void) {
58+
aps.smoothing = 0.5;
59+
aps_init(&aps);
60+
61+
ASSERT_IN_RANGE(0.5, aps.smoothing, 0.001);
62+
ASSERT_EQ(0, aps.value);
63+
ASSERT_EQ(0.0, aps.current);
64+
65+
PASS();
66+
}
67+
68+
TEST test_aps_init_start(void) {
69+
aps_init(&aps);
70+
71+
ASSERT(aps.start != 0);
72+
73+
PASS();
74+
}
75+
76+
TEST test_aps_reset_clears_value_and_current(void) {
77+
aps_init(&aps);
78+
79+
aps_update(&aps, 1);
80+
aps_update(&aps, 1);
81+
aps_update(&aps, 1);
82+
83+
/* Make sure value accumulated before reset. */
84+
ASSERT_EQ(3, aps.value);
85+
86+
aps_reset(&aps);
87+
88+
/* Reset should make them zero again. */
89+
ASSERT_EQ(0, aps.value);
90+
ASSERT_EQ(0.0, aps.current);
91+
92+
PASS();
93+
}
94+
95+
TEST test_aps_reset_preserves_smoothing(void) {
96+
aps.smoothing = 0.75;
97+
aps_init(&aps);
98+
99+
ASSERT_IN_RANGE(0.75, aps.smoothing, 0.001);
100+
101+
aps_update(&aps, 1);
102+
aps_reset(&aps);
103+
104+
ASSERT_IN_RANGE(0.75, aps.smoothing, 0.001);
105+
106+
PASS();
107+
}
108+
109+
TEST test_aps_reset_refreshes_start(void) {
110+
clock_t original;
111+
clock_t elapsed;
112+
113+
aps_init(&aps);
114+
original = aps.start;
115+
116+
/* Spin until the clock advances at least 1 tick. */
117+
do {
118+
elapsed = clock();
119+
} while (elapsed <= original + 1);
120+
121+
aps_reset(&aps);
122+
123+
ASSERT(aps.start != original);
124+
125+
PASS();
126+
}
127+
128+
TEST test_aps_update_increments_value(void) {
129+
uint32_t i;
130+
131+
aps_init(&aps);
132+
133+
for (i = 1; i <= 10; i++) {
134+
aps_update(&aps, 1);
135+
ASSERT_EQ(i, aps.value);
136+
}
137+
138+
PASS();
139+
}
140+
141+
SUITE(aps_suite) {
142+
SET_SETUP(setup_callback, NULL);
143+
RUN_TEST(test_aps_init_defaults);
144+
RUN_TEST(test_aps_init_custom_smoothing);
145+
RUN_TEST(test_aps_init_start);
146+
RUN_TEST(test_aps_reset_clears_value_and_current);
147+
RUN_TEST(test_aps_reset_preserves_smoothing);
148+
RUN_TEST(test_aps_reset_refreshes_start);
149+
RUN_TEST(test_aps_update_increments_value);
150+
}
151+
152+
GREATEST_MAIN_DEFS();
153+
154+
int main(int argc, char **argv) {
155+
GREATEST_MAIN_BEGIN();
156+
RUN_SUITE(aps_suite);
157+
GREATEST_MAIN_END();
158+
}

0 commit comments

Comments
 (0)