1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

math.big: make is_odd public and add test cases (#18916)

This commit is contained in:
phoebe 2023-07-20 12:52:50 +02:00 committed by GitHub
parent ef1f5d7725
commit 93b3f1ca55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -118,6 +118,14 @@ const is_power_of_2_test_data = [
IsXTest{ "4611686018427388002", false },
IsXTest{ "31195165372897259196222538898096203590151924108450147950531565441852619837316692843188389598728651769482088968838700984268947453885587967878549286444999755742573423371025356539077075265986419171772426279084559025861175301940492273427120221755816136975739916983004778387946699939545354293487098252428954036286183995782377175227121587657233553706589448547148066273280603243167958729707736664649187444136702017299877489729451997277875868782399735511520086969969766278182145454186690598629675562422923132555707758646587702550600894625696538109646366308973392363200122154242784576162149305816215109893613161331026672647000825615987247035266514313689413563779184515427920269935280569035788081552413007563772309295149800172031645681720569680154349893907395864528243629654386620034655445226295834594630792819545156798270599481573436039129275439653984521135652249263653985326577886990615665734998585216581730937090703518997669223802429711292740491797911117308280939507973715877108492303860661291987529284719391551256912380499409630332506454532263266457209921483705507359152839264852808182519011100934922492651373859423833024010283468753147686188675294998119637462200763443029190704825719342806119404339670408160210011918981038977425180213726646978883378058838510330816291941879581568740273684084511318422175006728346276489384220596694727036836687670632486602655240593463885077059375085482211864761344849868123074687509143827139683659102930877963676911995751113159944160296419825178911962487549670296207457410515598040046860567719116506974858703739531721991704589155513182996455827177472", true },
]
const is_odd_test_data = [
IsXTest{ u32(0), false },
IsXTest{ u32(1), true },
IsXTest{ u32(1805), true },
IsXTest{ "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", true },
IsXTest{ "57495732561923751347562394571325712358054", false },
]
// vfmt on
struct AddTest {
@ -514,6 +522,12 @@ fn test_is_power_of_2() {
}
}
fn test_is_odd() {
for t in is_odd_test_data {
assert t.value.parse().is_odd() == t.expected
}
}
fn test_addition() {
for t in add_test_data {
assert t.augend.parse() + t.addend.parse() == t.sum.parse()

View File

@ -1086,9 +1086,11 @@ fn (x Integer) rsh_to_set_bit() (Integer, u32) {
return x.right_shift(n), n
}
// is_odd returns true if the integer `x` is odd, therefore an integer of the form `2k + 1`.
// An input of 0 returns false.
[direct_array_access; inline]
fn (x Integer) is_odd() bool {
return x.digits[0] & 1 == 1
pub fn (x Integer) is_odd() bool {
return x.digits.len != 0 && x.digits[0] & 1 == 1
}
// is_power_of_2 returns true when the integer `x` satisfies `2^n`, where `n >= 0`