@@ -29,6 +29,17 @@ const uint8_t gau8Input1[] = {0x80, 0xff};
2929const SBitGenInitParam gsParam1 = {false, 1 , 0 };
3030const 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+
3243typedef 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+
6880bool 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+
88116bool 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
131210int 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}
0 commit comments