@@ -163,6 +163,75 @@ static void test_amount_with_fee(void)
163163 2100000001234567890ULL );
164164}
165165
166+ static void test_case_amount_div (u64 input , u64 div , u64 expected )
167+ {
168+ struct amount_msat msat = amount_msat (input );
169+ struct amount_msat expected_msat = amount_msat (expected );
170+ struct amount_msat result_msat = amount_msat_div (msat , div );
171+ assert (amount_msat_eq (result_msat , expected_msat ));
172+ }
173+
174+ static void test_case_amount_div_ceil (u64 input , u64 div , u64 expected )
175+ {
176+ struct amount_msat msat = amount_msat (input );
177+ struct amount_msat expected_msat = amount_msat (expected );
178+ struct amount_msat result_msat = amount_msat_div_ceil (msat , div );
179+ assert (amount_msat_eq (result_msat , expected_msat ));
180+ }
181+
182+ static void test_amount_div (void )
183+ {
184+ test_case_amount_div (1 , 1 , 1 );
185+ test_case_amount_div (1 , 2 , 0 );
186+ test_case_amount_div (1 , 3 , 0 );
187+
188+ test_case_amount_div (2 , 1 , 2 );
189+ test_case_amount_div (2 , 2 , 1 );
190+ test_case_amount_div (2 , 3 , 0 );
191+
192+ test_case_amount_div (3 , 1 , 3 );
193+ test_case_amount_div (3 , 2 , 1 );
194+ test_case_amount_div (3 , 3 , 1 );
195+ test_case_amount_div (3 , 4 , 0 );
196+
197+ test_case_amount_div (10 , 1 , 10 );
198+ test_case_amount_div (10 , 2 , 5 );
199+ test_case_amount_div (10 , 3 , 3 );
200+ test_case_amount_div (10 , 4 , 2 );
201+ test_case_amount_div (10 , 5 , 2 );
202+ test_case_amount_div (10 , 6 , 1 );
203+ test_case_amount_div (10 , 7 , 1 );
204+ test_case_amount_div (10 , 8 , 1 );
205+ test_case_amount_div (10 , 9 , 1 );
206+ test_case_amount_div (10 , 10 , 1 );
207+ test_case_amount_div (10 , 11 , 0 );
208+
209+ test_case_amount_div_ceil (1 , 1 , 1 );
210+ test_case_amount_div_ceil (1 , 2 , 1 );
211+ test_case_amount_div_ceil (1 , 3 , 1 );
212+
213+ test_case_amount_div_ceil (2 , 1 , 2 );
214+ test_case_amount_div_ceil (2 , 2 , 1 );
215+ test_case_amount_div_ceil (2 , 3 , 1 );
216+
217+ test_case_amount_div_ceil (3 , 1 , 3 );
218+ test_case_amount_div_ceil (3 , 2 , 2 );
219+ test_case_amount_div_ceil (3 , 3 , 1 );
220+ test_case_amount_div_ceil (3 , 4 , 1 );
221+
222+ test_case_amount_div_ceil (10 , 1 , 10 );
223+ test_case_amount_div_ceil (10 , 2 , 5 );
224+ test_case_amount_div_ceil (10 , 3 , 4 );
225+ test_case_amount_div_ceil (10 , 4 , 3 );
226+ test_case_amount_div_ceil (10 , 5 , 2 );
227+ test_case_amount_div_ceil (10 , 6 , 2 );
228+ test_case_amount_div_ceil (10 , 7 , 2 );
229+ test_case_amount_div_ceil (10 , 8 , 2 );
230+ test_case_amount_div_ceil (10 , 9 , 2 );
231+ test_case_amount_div_ceil (10 , 10 , 1 );
232+ test_case_amount_div_ceil (10 , 11 , 1 );
233+ }
234+
166235#define FAIL_MSAT (msatp , str ) \
167236 assert(!parse_amount_msat((msatp), (str), strlen(str)))
168237#define PASS_MSAT (msatp , str , val ) \
@@ -330,5 +399,6 @@ int main(int argc, char *argv[])
330399 }
331400
332401 test_amount_with_fee ();
402+ test_amount_div ();
333403 common_shutdown ();
334404}
0 commit comments