Skip to content

Commit f6e7ab8

Browse files
committed
[ISSUE-10]: generators fully covered by tests
1 parent 1868333 commit f6e7ab8

6 files changed

Lines changed: 171 additions & 21 deletions

File tree

src/utils/generators.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,26 @@ void pwmgen_reset(SPwmGenState *psState) {
163163
psState->u8PhaseIdx = PWMPHASE_END;
164164
}
165165

166-
// (Faster) PWM Generator section
166+
// (Faster) 16bit PWM Generator section
167+
168+
SBitPwmGenState bitpwmgen_init(uint8_t u8HiUpper, uint8_t u8LoUpper, uint8_t u8PeriodLen,
169+
bool bUp, uint8_t u8HiLower, uint8_t u8LoLower,
170+
size_t szInputLen, uint8_t *pu8Input) {
171+
SBitPwmGenState sBPGState = {
172+
.sBitGenState = bitgen_init(0x00, bUp, u8HiLower, u8LoLower),
173+
.sByteGenState = bytegen_init(pu8Input, szInputLen),
174+
.sPwmXGenState = (SPwmXGenState)
175+
{
176+
.u8CurValue = 0x01, // ignored
177+
.u8HiUpper = u8HiUpper,
178+
.u8LoUpper = u8LoUpper,
179+
.u8PeriodLen = u8PeriodLen,
180+
.u8PhaseIdx = 2 // to force reset at first call of ~_next()
181+
}
182+
};
183+
sBPGState.sBitGenState.u8BitIdx = 8; // to force reset at first call of ~_next()
184+
return sBPGState;
185+
}
167186

168187
uint16_t bitpwmgen_next(SBitPwmGenState *psState) {
169188
SPwmXGenState *px = &psState->sPwmXGenState; // alias, for writing shorter lines

src/utils/generators.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern "C" {
5858
union {
5959
FToXReset fReset;
6060
FByteToXResetV fResetV;
61-
};
61+
} ;
6262
} SToByteFunctions;
6363

6464
typedef struct {
@@ -68,7 +68,7 @@ extern "C" {
6868
union {
6969
FToXReset fReset;
7070
FWordToXResetV fResetV;
71-
};
71+
} ;
7272
} SToWordFunctions;
7373

7474
/**
@@ -154,6 +154,9 @@ extern "C" {
154154
bool pwmgen_end(const SPwmGenState *psState);
155155
void pwmgen_reset(SPwmGenState *psState);
156156

157+
SBitPwmGenState bitpwmgen_init(uint8_t u8HiUpper, uint8_t u8LoUpper, uint8_t u8PeriodLen,
158+
bool bUp, uint8_t u8HiLower, uint8_t u8LoLower,
159+
size_t szInputLen, uint8_t *pu8Input);
157160
uint16_t bitpwmgen_next(SBitPwmGenState *psState);
158161
bool bitpwmgen_end(const SBitPwmGenState *psState);
159162
void bitpwmgen_reset(SBitPwmGenState *psState);

tests/bitgen_test.c

Lines changed: 122 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ const uint8_t gau8Input1[] = {0x80, 0xff};
2929
const SBitGenInitParam gsParam1 = {false, 1, 0};
3030
const uint8_t gau8Expected1[] = {1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
3131

32+
const uint8_t gau8Input3[] = {0x80, 0x00, 0xA5, 0xA5};
33+
const uint8_t gau8ResetAt3[] = {6, 8, 10, 12, 14};
34+
const uint8_t gau8GenLen3[] = {21, 21, 21, 21, 21};
35+
const uint8_t gau8Expected3[5][21] = {
36+
{1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0},
37+
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0},
38+
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1},
39+
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1},
40+
{1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0}
41+
};
42+
3243
typedef struct {
3344
const uint8_t *pu8Begin;
3445
const uint8_t *pu8End;
@@ -65,6 +76,7 @@ uint8_t bitsgen_next(SBitSGenState *psState) {
6576
++psState->u8BitIdx;
6677
return bBitHi ? psState->u8OutHi : psState->u8OutLo;
6778
}
79+
6880
bool bitsgen_end(const SBitSGenState *psState) {
6981
return (psState->pu8Cur == psState->pu8End) && (psState->u8BitIdx == 8);
7082
}
@@ -85,6 +97,22 @@ bool test_bitgen_iter(SBitGenInitParam sParam, uint8_t u8Input, const uint8_t *p
8597
return gentest_check_seq8_equal(gsBitGenFunc, &sGen, pu8Exp, 8);
8698
}
8799

100+
// after reset the stream must be continued from begin
101+
102+
bool test_bitgen_reset(SBitGenInitParam sParam, uint8_t u8Input, uint8_t u8ResetAt, const uint8_t *pu8Exp) {
103+
SBitGenState sGen = bitgen_init(u8Input, sParam.bLsbFirst, sParam.u8High, sParam.u8Low);
104+
uint8_t au8Actual[8];
105+
uint8_t i = 0;
106+
for (; i < u8ResetAt; ++i) {
107+
au8Actual[i] = bitgen_next(&sGen);
108+
}
109+
bitgen_resetv(&sGen, u8Input);
110+
for (; i < 8; ++i) {
111+
au8Actual[i] = bitgen_next(&sGen);
112+
}
113+
check_seq8_equal(au8Actual, pu8Exp, 8);
114+
}
115+
88116
bool test_bitseqgen_iter(SBitGenInitParam sParam, const uint8_t *pu8Input, size_t szInputLen, const uint8_t *pu8Expected) {
89117

90118
SBitGenState sBitGen = bitgen_init(0, sParam.bLsbFirst, sParam.u8High, sParam.u8Low);
@@ -125,26 +153,113 @@ bool test_bitsgen_reset(SBitGenInitParam sParam, const uint8_t *pu8Input, size_t
125153
return bRet;
126154
}
127155

128-
uint8_t au8Input2[1000];
129-
uint8_t au8Exp2[8000];
156+
bool test_bitsgen_reset2(SBitGenInitParam sParam, const uint8_t *pu8Input, size_t szInputLen, const uint8_t *pu8Expected) {
157+
bool bRet = true;
158+
159+
for (unsigned int i = 0; i < 8 * szInputLen; ++i) {
160+
SBitSGenState sBitSGen = bitsgen_init(pu8Input, szInputLen, sParam.bLsbFirst, sParam.u8High, sParam.u8Low);
161+
gentest_reset_after_steps(&sBitSGen, (FToByteNext) bitsgen_next, (FToXReset) bitsgen_reset, i);
162+
bRet &= gentest_check_seq8_equal(gsBitSGenFunc, &sBitSGen, pu8Expected, 8 * szInputLen);
163+
}
164+
return bRet;
165+
}
166+
167+
bool test_bitpwmgen_iter(SBitPwmGenState *psBPGState, size_t szExpLen, uint16_t *pu16Exp) {
168+
bool bRet = true;
169+
for (size_t i = 0; i < szExpLen; ++i) {
170+
if (bitpwmgen_end(psBPGState)) {
171+
fprintf(stderr, "BitPwmGen premature end detected after output %zu\n", i);
172+
bRet = false;
173+
break;
174+
}
175+
uint16_t u16Val = bitpwmgen_next(psBPGState);
176+
if (pu16Exp[i] != u16Val) {
177+
fprintf(stderr, "BitPwmGen value mismatch at pos %zu (expected: %u, actual: %u)\n", i, pu16Exp[i], u16Val);
178+
bRet = false;
179+
}
180+
}
181+
if (!bitpwmgen_end(psBPGState)) {
182+
fprintf(stderr, "BitPwmGen end missing\n");
183+
bRet = false;
184+
}
185+
return bRet;
186+
}
187+
188+
bool test_bitpwmgen_reset(SBitPwmGenState *psBPGState, uint32_t u32ResetAt, size_t szGenLen, uint16_t *pu16Exp) {
189+
bool bRet = true;
190+
191+
size_t i = 0;
192+
for (; i < u32ResetAt; ++i) {
193+
bitpwmgen_next(psBPGState);
194+
}
195+
bitpwmgen_reset(psBPGState);
196+
197+
for (; i < szGenLen; ++i) {
198+
uint16_t u16Val = bitpwmgen_next(psBPGState);
199+
if (pu16Exp[i] != u16Val) {
200+
fprintf(stderr, "test_bitpwmgen_reset() value mismatch at pos %zu (expected: %u, actual: %u)\n", i, pu16Exp[i], u16Val);
201+
bRet = false;
202+
}
203+
}
204+
return bRet;
205+
}
206+
207+
uint8_t gau8Input2[1000];
208+
uint8_t gau8Exp2[8000];
130209

131210
int main(int argc, char **argv) {
132211

133212
if (1 < argc) {
134213
fprintf(stderr, "%s does not require command line arguments\n", argv[0]);
135214
}
136215

216+
// bitgen tests
137217
assert(test_bitgen_iter(gasParam[0], gu8Input0, gau8Exp0_0));
138218
assert(test_bitgen_iter(gasParam[1], gu8Input0, gau8Exp0_1));
139219
assert(test_bitgen_iter(gasParam[2], gu8Input0, gau8Exp0_2));
140220

141-
// assert(test_bitseqgen_iter(gsParam1, gau8Input1, ARRAY_SIZE(gau8Input1), gau8Expected1));
142-
// assert(test_bitseqgen_reset(gsParam1, gau8Input1, ARRAY_SIZE(gau8Input1), gau8Expected1));
221+
uint8_t au8BitgenResetExp[3][8] = {
222+
{1, 1, 1, 1, 0, 0, 0, 0},
223+
{1, 1, 1, 1, 1, 0, 0, 0},
224+
{1, 1, 1, 1, 0, 0, 0, 1}
225+
};
226+
assert(test_bitgen_reset(gasParam[0], 0xF0, 0, au8BitgenResetExp[0]));
227+
assert(test_bitgen_reset(gasParam[0], 0xF0, 1, au8BitgenResetExp[1]));
228+
assert(test_bitgen_reset(gasParam[0], 0xF0, 7, au8BitgenResetExp[2]));
229+
230+
// bitseqgen tests
231+
assert(test_bitseqgen_iter(gsParam1, gau8Input1, ARRAY_SIZE(gau8Input1), gau8Expected1));
232+
assert(test_bitseqgen_reset(gsParam1, gau8Input1, ARRAY_SIZE(gau8Input1), gau8Expected1));
233+
143234
assert(test_bitsgen_iter(gsParam1, gau8Input1, ARRAY_SIZE(gau8Input1), gau8Expected1));
144235
assert(test_bitsgen_reset(gsParam1, gau8Input1, ARRAY_SIZE(gau8Input1), gau8Expected1));
145236

146-
memset(au8Input2, 0xff, sizeof (au8Input2));
147-
memset(au8Exp2, 0x40, sizeof (au8Exp2));
148-
assert(test_bitsgen_reset((SBitGenInitParam){false, 0x40, 0x01}, au8Input2, ARRAY_SIZE(au8Input2), au8Exp2));
237+
memset(gau8Input2, 0xff, sizeof (gau8Input2));
238+
memset(gau8Exp2, 0x40, sizeof (gau8Exp2));
239+
assert(test_bitsgen_reset((SBitGenInitParam){false, 0x40, 0x01}, gau8Input2, ARRAY_SIZE(gau8Input2), gau8Exp2));
240+
241+
242+
// bitpwmgen tests
243+
uint8_t au8BitPwmGenInput[] = {0x00, 0xFF, 0xF0, 0x0F};
244+
SBitPwmGenState sBitPwmGenState = bitpwmgen_init(0x80, 0x00, 0x40, false, 0x2A, 0x10, ARRAY_SIZE(au8BitPwmGenInput), au8BitPwmGenInput);
245+
uint16_t au16BitPwmGenExp[] = {
246+
0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030,
247+
0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016,
248+
0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030,
249+
0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016
250+
};
251+
assert(test_bitpwmgen_iter(&sBitPwmGenState, 16 * ARRAY_SIZE(au8BitPwmGenInput), au16BitPwmGenExp));
252+
253+
uint32_t u32BitPwmGen2ResetAt = 19;
254+
SBitPwmGenState sBitPwmGen2State = bitpwmgen_init(0x80, 0x00, 0x40, false, 0x2A, 0x10, ARRAY_SIZE(au8BitPwmGenInput), au8BitPwmGenInput);
255+
uint16_t au16BitPwmGen2Exp[] = {
256+
0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030,
257+
0x802A, 0x0016, 0x802A,
258+
0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030, 0x8010, 0x0030,
259+
0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016, 0x802A, 0x0016,
260+
};
261+
assert(test_bitpwmgen_reset(&sBitPwmGen2State, u32BitPwmGen2ResetAt, ARRAY_SIZE(au16BitPwmGen2Exp), au16BitPwmGen2Exp));
262+
263+
149264
return 0;
150265
}

tests/bytegen_test.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ const STextAndLen gasInput[]={
1919
{TEXTANDLEN("")}
2020
};
2121

22-
uint8_t gau8Input1[] = {3, 5, 7, 9};
23-
uint8_t gau8Param1[] = {10, 0x80, 0x00};
24-
uint16_t gau16Expected1[] = {0x8003, 0x0007, 0x8005, 0x0005, 0x8007, 0x0003, 0x8009, 0x0001};
22+
uint8_t gau8PwmInput1[] = {3, 5, 7, 9}; // pwm high signal lengths
23+
uint8_t gau8PwmParam1[] = {10, 0x80, 0x00}; // period length, high signal upper byte, low signal upper byte
24+
uint16_t gau16PwmExpected1[] = {0x8003, 0x0007, 0x8005, 0x0005, 0x8007, 0x0003, 0x8009, 0x0001};
2525

2626
bool test_bytegen_iter(const STextAndLen sParam) {
2727
SByteGenState sGen = bytegen_init((uint8_t*) sParam.sText, sParam.zLen);
@@ -65,6 +65,6 @@ int main(int argc, char **argv) {
6565
assert(test_bytegen_reset(gasInput[0]));
6666
assert(test_bytegen_iter(gasInput[1]));
6767
assert(test_bytegen_reset(gasInput[1]));
68-
assert(test_pwngen_iter(gau8Input1, ARRAY_SIZE(gau8Input1), gau16Expected1, gau8Param1[0], gau8Param1[1], gau8Param1[2]));
69-
assert(test_pwmgen_reset(gau8Input1, ARRAY_SIZE(gau8Input1), gau16Expected1, gau8Param1[0], gau8Param1[1], gau8Param1[2]));
68+
assert(test_pwngen_iter(gau8PwmInput1, ARRAY_SIZE(gau8PwmInput1), gau16PwmExpected1, gau8PwmParam1[0], gau8PwmParam1[1], gau8PwmParam1[2]));
69+
assert(test_pwmgen_reset(gau8PwmInput1, ARRAY_SIZE(gau8PwmInput1), gau16PwmExpected1, gau8PwmParam1[0], gau8PwmParam1[1], gau8PwmParam1[2]));
7070
}

tests/gentest_common.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,35 @@
33
#include <stdbool.h>
44
#include "gentest_common.h"
55

6+
bool check_seq8_equal(const uint8_t *pu8Act, const uint8_t *pu8Exp, size_t szLen) {
7+
bool bRet = true;
8+
for (size_t i = 0; i < szLen; ++i) {
9+
if (pu8Act[i] != pu8Exp[i]) {
10+
fprintf(stderr, "Array differ at index %zu (actual: %u, expected: %u)\n", i, pu8Act[i], pu8Exp[i]);
11+
bRet = false;
12+
break;
13+
}
14+
}
15+
return bRet;
16+
}
17+
618
bool gentest_check_seq8_equal(const SToByteFunctions sFunc, void *pvSeqGen, const uint8_t *pu8Exp, size_t szExpLen) {
719
bool bRet = true;
820
for (size_t i = 0; i < szExpLen; ++i) {
921
bool bEnd = sFunc.fEnd(pvSeqGen);
1022
uint8_t u8Val = sFunc.fNext(pvSeqGen);
1123
if (bEnd) {
12-
fprintf(stderr, "generator marks end too early: %zu vs. %zu\n", i, szExpLen);
24+
fprintf(stderr, "Generator marks end too early: %zu vs. %zu\n", i, szExpLen);
1325
bRet = false;
1426
}
1527
if (u8Val != pu8Exp[i]) {
16-
fprintf(stderr, "bit mismatch at position #%zu: %u vs. %u\n", i, u8Val, pu8Exp[i]);
28+
fprintf(stderr, "Bit mismatch at position #%zu: %u vs. %u\n", i, u8Val, pu8Exp[i]);
1729
bRet = false;
1830
}
1931
}
2032
bool bEndOk = sFunc.fEnd(pvSeqGen);
2133
if (!bEndOk) {
22-
fprintf(stderr, "generator marks end too late: %zu\n", szExpLen);
34+
fprintf(stderr, "Generator marks end too late: %zu\n", szExpLen);
2335
bRet = false;
2436
}
2537
return bRet;
@@ -31,17 +43,17 @@ bool gentest_check_seq16_equal(const SToWordFunctions sFunc, void *pvSeqGen, con
3143
bool bEnd = sFunc.fEnd(pvSeqGen);
3244
uint16_t u16Val = sFunc.fNext(pvSeqGen);
3345
if (bEnd) {
34-
fprintf(stderr, "generator marks end too early: %zu vs. %zu\n", i, szExpLen);
46+
fprintf(stderr, "Generator marks end too early: %zu vs. %zu\n", i, szExpLen);
3547
bRet = false;
3648
}
3749
if (u16Val != pu16Exp[i]) {
38-
fprintf(stderr, "bit mismatch at position #%zu: %u vs. %u\n", i, u16Val, pu16Exp[i]);
50+
fprintf(stderr, "Bit mismatch at position #%zu: %u vs. %u\n", i, u16Val, pu16Exp[i]);
3951
bRet = false;
4052
}
4153
}
4254
bool bEndOk = sFunc.fEnd(pvSeqGen);
4355
if (!bEndOk) {
44-
fprintf(stderr, "generator marks end too late: %zu\n", szExpLen);
56+
fprintf(stderr, "Generator marks end too late: %zu\n", szExpLen);
4557
bRet = false;
4658
}
4759
return bRet;

tests/gentest_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414
fReset(pvState);
1515
}
1616

17+
bool check_seq8_equal(const uint8_t *pu8Act, const uint8_t *pu8Exp, size_t szLen);
1718
bool gentest_check_seq8_equal(const SToByteFunctions sFunc, void *pvSeqGen, const uint8_t *pu8Exp, size_t szExpLen);
1819
bool gentest_check_seq16_equal(const SToWordFunctions sFunc, void *pvSeqGen, const uint16_t *pu16Exp, size_t szExpLen);
1920

0 commit comments

Comments
 (0)