3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
9
10#include <Arduino.h>
11#include <unity.h>
12
13#include "../halt_after_tests.h"
14
15#include "FastDivision.h"
16#include "FastDivision.cpp" // Include implementation for linking
17
18void setUp(void)
19{
20}
21
22void tearDown(void)
23{
24}
25
26// ============================================================================
27// Tests for divu10 (AVR assembly implementation)
28// ============================================================================
29
31{
32 TEST_ASSERT_EQUAL(0, divu10(0)); // 0 / 10 = 0
33 TEST_ASSERT_EQUAL(1, divu10(10)); // 10 / 10 = 1
34 TEST_ASSERT_EQUAL(2, divu10(20)); // 20 / 10 = 2
35 TEST_ASSERT_EQUAL(10, divu10(100)); // 100 / 10 = 10
36}
37
39{
40 TEST_ASSERT_EQUAL(0, divu10(1)); // 1 / 10 = 0
41 TEST_ASSERT_EQUAL(0, divu10(9)); // 9 / 10 = 0
42 TEST_ASSERT_EQUAL(1, divu10(11)); // 11 / 10 = 1
43 TEST_ASSERT_EQUAL(1, divu10(19)); // 19 / 10 = 1
44}
45
47{
48 TEST_ASSERT_EQUAL(100, divu10(1000)); // 1000 / 10 = 100
49 TEST_ASSERT_EQUAL(1000, divu10(10000)); // 10000 / 10 = 1000
50 TEST_ASSERT_EQUAL(6553, divu10(65535)); // max uint16_t / 10 = 6553
51}
52
54{
55 TEST_ASSERT_EQUAL(25, divu10(250)); // 250 / 10 = 25
56 TEST_ASSERT_EQUAL(123, divu10(1234)); // 1234 / 10 = 123
57 TEST_ASSERT_EQUAL(42, divu10(425)); // 425 / 10 = 42
58 TEST_ASSERT_EQUAL(99, divu10(999)); // 999 / 10 = 99
59}
60
61// ============================================================================
62// Tests for divmod10 (AVR assembly implementation)
63// ============================================================================
64
66{
67 uint32_t div;
68 uint8_t mod;
69
70 divmod10(0, div, mod);
71 TEST_ASSERT_EQUAL(0, div);
72 TEST_ASSERT_EQUAL(0, mod);
73
74 divmod10(10, div, mod);
75 TEST_ASSERT_EQUAL(1, div);
76 TEST_ASSERT_EQUAL(0, mod);
77
78 divmod10(100, div, mod);
79 TEST_ASSERT_EQUAL(10, div);
80 TEST_ASSERT_EQUAL(0, mod);
81}
82
84{
85 uint32_t div;
86 uint8_t mod;
87
88 divmod10(1, div, mod);
89 TEST_ASSERT_EQUAL(0, div);
90 TEST_ASSERT_EQUAL(1, mod);
91
92 divmod10(9, div, mod);
93 TEST_ASSERT_EQUAL(0, div);
94 TEST_ASSERT_EQUAL(9, mod);
95
96 divmod10(15, div, mod);
97 TEST_ASSERT_EQUAL(1, div);
98 TEST_ASSERT_EQUAL(5, mod);
99
100 divmod10(99, div, mod);
101 TEST_ASSERT_EQUAL(9, div);
102 TEST_ASSERT_EQUAL(9, mod);
103}
104
106{
107 uint32_t div;
108 uint8_t mod;
109
110 divmod10(12345, div, mod);
111 TEST_ASSERT_EQUAL(1234, div);
112 TEST_ASSERT_EQUAL(5, mod);
113
114 divmod10(1000000, div, mod);
115 TEST_ASSERT_EQUAL(100000, div);
116 TEST_ASSERT_EQUAL(0, mod);
117}
118
119// ============================================================================
120// Tests for constexpr shift-based divisions
121// ============================================================================
122
123void test_divu8(void)
124{
125 TEST_ASSERT_EQUAL(0, divu8(0)); // 0 / 8 = 0
126 TEST_ASSERT_EQUAL(0, divu8(7)); // 7 / 8 = 0
127 TEST_ASSERT_EQUAL(1, divu8(8)); // 8 / 8 = 1
128 TEST_ASSERT_EQUAL(1, divu8(15)); // 15 / 8 = 1
129 TEST_ASSERT_EQUAL(2, divu8(16)); // 16 / 8 = 2
130 TEST_ASSERT_EQUAL(125, divu8(1000)); // 1000 / 8 = 125
131 TEST_ASSERT_EQUAL(8191, divu8(65535)); // 65535 / 8 = 8191
132}
133
134void test_divu4(void)
135{
136 TEST_ASSERT_EQUAL(0, divu4(0)); // 0 / 4 = 0
137 TEST_ASSERT_EQUAL(0, divu4(3)); // 3 / 4 = 0
138 TEST_ASSERT_EQUAL(1, divu4(4)); // 4 / 4 = 1
139 TEST_ASSERT_EQUAL(1, divu4(7)); // 7 / 4 = 1
140 TEST_ASSERT_EQUAL(2, divu4(8)); // 8 / 4 = 2
141 TEST_ASSERT_EQUAL(250, divu4(1000)); // 1000 / 4 = 250
142 TEST_ASSERT_EQUAL(16383, divu4(65535)); // 65535 / 4 = 16383
143}
144
145void test_divu2(void)
146{
147 TEST_ASSERT_EQUAL(0, divu2(0)); // 0 / 2 = 0
148 TEST_ASSERT_EQUAL(0, divu2(1)); // 1 / 2 = 0
149 TEST_ASSERT_EQUAL(1, divu2(2)); // 2 / 2 = 1
150 TEST_ASSERT_EQUAL(1, divu2(3)); // 3 / 2 = 1
151 TEST_ASSERT_EQUAL(500, divu2(1000)); // 1000 / 2 = 500
152 TEST_ASSERT_EQUAL(32767, divu2(65535)); // 65535 / 2 = 32767
153}
154
155void test_divu1(void)
156{
157 TEST_ASSERT_EQUAL(0, divu1(0));
158 TEST_ASSERT_EQUAL(1, divu1(1));
159 TEST_ASSERT_EQUAL(1000, divu1(1000));
160 TEST_ASSERT_EQUAL(65535, divu1(65535));
161}
162
163// ============================================================================
164// Main
165// ============================================================================
166
167void setup()
168{
169 delay(1000);
170 UNITY_BEGIN();
171}
172
173void loop()
174{
175 // divu10 tests
176 RUN_TEST(test_divu10_basic);
177 RUN_TEST(test_divu10_edge_cases);
178 RUN_TEST(test_divu10_large_values);
180
181 // divmod10 tests
182 RUN_TEST(test_divmod10_basic);
185
186 // Shift-based division tests
187 RUN_TEST(test_divu8);
188 RUN_TEST(test_divu4);
189 RUN_TEST(test_divu2);
190 RUN_TEST(test_divu1);
191
192 UNITY_END();
194}
void divmod10(uint32_t in, uint32_t &div, uint8_t &mod)
uint16_t divu10(uint16_t n)
constexpr auto divu1(uint16_t n)
constexpr auto divu8(uint16_t n)
Definition FastDivision.h:9
constexpr auto divu2(uint16_t n)
constexpr auto divu4(uint16_t n)
void setup()
void setUp(void)
Definition test_main.cpp:27
void tearDown(void)
Definition test_main.cpp:28
void loop()
void test_divmod10_with_remainder(void)
Definition test_main.cpp:83
void test_divu10_basic(void)
Definition test_main.cpp:30
void test_divu1(void)
void test_divu4(void)
void test_divmod10_basic(void)
Definition test_main.cpp:65
void test_divmod10_large_values(void)
void test_divu8(void)
void test_divu10_large_values(void)
Definition test_main.cpp:46
void test_divu10_random_values(void)
Definition test_main.cpp:53
void test_divu10_edge_cases(void)
Definition test_main.cpp:38
void test_divu2(void)
Stops the MCU once an embedded test suite has run.
void haltAfterTests()