Skip to content

Commit 4c503c4

Browse files
Merge pull request #20 from jeromekelleher/more-tests-c
Fixup itoa testing
2 parents a0b5342 + 10a6f46 commit 4c503c4

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,29 @@ jobs:
6666
# Make sure we don't have ``vcztools`` in the CWD
6767
cd tests
6868
python -m vcztools --help
69+
70+
c_test:
71+
name: C tests
72+
runs-on: ubuntu-latest
73+
steps:
74+
- uses: actions/checkout@v4
75+
- name: Install dependencies
76+
run: |
77+
sudo apt install -y ninja-build libcunit1-dev valgrind meson gcovr
78+
- name: Build
79+
working-directory: ./lib
80+
run: |
81+
meson setup -Db_coverage=true build
82+
- name: Tests
83+
working-directory: ./lib
84+
run: |
85+
ninja -C build test
86+
- name: Show coverage
87+
working-directory: ./lib
88+
run: |
89+
ninja -C build coverage-text
90+
cat build/meson-logs/coverage.txt
91+
- name: Valgrind
92+
working-directory: ./lib
93+
run: |
94+
valgrind --leak-check=full --error-exitcode=1 ./build/tests

lib/tests.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ test_variant_encoder_fields_all_missing(void)
245245
vcz_variant_encoder_free(&writer);
246246
}
247247

248-
// TODO also do this with larger values to hit all cases.
249248
static void
250249
test_itoa_small(void)
251250
{
@@ -262,6 +261,45 @@ test_itoa_small(void)
262261
}
263262
}
264263

264+
static void
265+
test_itoa_pow10(void)
266+
{
267+
char dest1[64], dest2[64];
268+
int len1, len2;
269+
int32_t j, k, power, value;
270+
271+
for (power = 0; power < 10; power++) {
272+
j = (int32_t) pow(10, power);
273+
for (k = -1; k < 2; k++) {
274+
value = j + k;
275+
len1 = sprintf(dest1, "%d", value);
276+
len2 = vcz_itoa(dest2, value);
277+
/* printf("%s %s\n", dest1, dest2); */
278+
CU_ASSERT_STRING_EQUAL_FATAL(dest1, dest2);
279+
CU_ASSERT_EQUAL(len1, len2);
280+
}
281+
}
282+
}
283+
284+
static void
285+
test_itoa_boundary(void)
286+
{
287+
char dest1[64], dest2[64];
288+
int len1, len2;
289+
size_t j;
290+
int32_t value;
291+
int32_t cases[] = { INT32_MIN, INT32_MAX };
292+
293+
for (j = 0; j < sizeof(cases) / sizeof(*cases); j++) {
294+
value = cases[j];
295+
len1 = sprintf(dest1, "%d", value);
296+
len2 = vcz_itoa(dest2, value);
297+
/* printf("%s %s\n", dest1, dest2); */
298+
CU_ASSERT_STRING_EQUAL(dest1, dest2);
299+
CU_ASSERT_EQUAL(len1, len2);
300+
}
301+
}
302+
265303
static void
266304
test_ftoa(void)
267305
{
@@ -407,6 +445,8 @@ main(int argc, char **argv)
407445
{ "test_variant_encoder_minimal", test_variant_encoder_minimal },
408446
{ "test_variant_fields_all_missing", test_variant_encoder_fields_all_missing },
409447
{ "test_itoa_small", test_itoa_small },
448+
{ "test_itoa_pow10", test_itoa_pow10 },
449+
{ "test_itoa_boundary", test_itoa_boundary },
410450
{ "test_ftoa", test_ftoa },
411451
{ NULL, NULL },
412452
};

lib/vcf_encoder.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#include <math.h>
99

1010
int
11-
vcz_itoa(char *buf, int32_t value)
11+
vcz_itoa(char *buf, int32_t v)
1212
{
13+
int64_t value = v;
1314
int p = 0;
1415
int j, k;
1516

@@ -40,6 +41,10 @@ vcz_itoa(char *buf, int32_t value)
4041
k = 7;
4142
} else if (value < 1000000000) {
4243
k = 8;
44+
} else if (value < 10000000000) {
45+
k = 9;
46+
} else {
47+
assert(false);
4348
}
4449

4550
// iterate backwards in buf
@@ -76,9 +81,9 @@ vcz_ftoa(char *buf, float value)
7681
return p + 3;
7782
}
7883

79-
/* integer part */
84+
/* integer part */
8085
i = (int64_t) round(((double) value) * 1000);
81-
p += vcz_itoa(buf + p, (int32_t) (i / 1000));
86+
p += vcz_itoa(buf + p, (int32_t)(i / 1000));
8287

8388
/* fractional part */
8489
d3 = i % 10;

tests/test_vcf_roundtrip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def vcz_path_cache(vcf_path):
1717
cache_path.mkdir()
1818
cached_vcz_path = (cache_path / vcf_path.name).with_suffix(".vcz")
1919
if not cached_vcz_path.exists():
20-
vcf2zarr.convert([vcf_path], cached_vcz_path, worker_processes=0)
20+
vcf2zarr.convert([vcf_path], cached_vcz_path, worker_processes=0, local_alleles=False)
2121
return cached_vcz_path
2222

2323

0 commit comments

Comments
 (0)