Skip to content

Commit

Permalink
[tst] Add unit test for humansize.c
Browse files Browse the repository at this point in the history
The test checks if conversion from bytes to human readable format
is correct.

Signed-off-by: Zuzana Miklankova <zmiklank@redhat.com>
  • Loading branch information
zmiklank authored and dcantrell committed Apr 25, 2023
1 parent 66b0953 commit 3d80445
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/lib/test-humansize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright The rpminspect Project Authors
* SPDX-License-Identifier: GPL-3.0-or-later
*/

#include <CUnit/Basic.h>
#include "rpminspect.h"

#include "test-main.h"


int init_test_humansize(void) {
return 0;
}

int clean_test_humansize(void) {
return 0;
}

void test_humansize(void) {
RI_ASSERT_EQUAL(strncmp(human_size(12), "12 B", 4), 0);
RI_ASSERT_EQUAL(strncmp(human_size(12288), "12 KiB", 6), 0);
RI_ASSERT_EQUAL(strncmp(human_size(12582912), "12 MiB", 6), 0);
RI_ASSERT_EQUAL(strncmp(human_size(3221225472), "3 GiB", 5), 0);
RI_ASSERT_NOT_EQUAL(strncmp(human_size(13), "12 B", 4), 0);
RI_ASSERT_NOT_EQUAL(strncmp(human_size(13288), "12 KiB", 6), 0);
RI_ASSERT_NOT_EQUAL(strncmp(human_size(13582912), "12 MiB", 6), 0);
RI_ASSERT_NOT_EQUAL(strncmp(human_size(2147483648), "3 GiB", 5), 0);
}

CU_pSuite get_suite(void) {
CU_pSuite pSuite = NULL;

/* add a suite to the registry */
pSuite = CU_add_suite("humansize", init_test_humansize, clean_test_humansize);
if (pSuite == NULL) {
return NULL;
}

/* add tests to the suite */
if (CU_add_test(pSuite, "test human_size()", test_humansize) == NULL) {
return NULL;
}

return pSuite;
}
11 changes: 11 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ if cunit.found()
link_with : [ librpminspect ],
)

test_humansize = executable(
'test-humansize',
['lib/test-humansize.c',
'lib/test-main.c'],
include_directories : inc,
dependencies : [ cunit, libkmod ],
c_args : '-D_BUILDDIR_="@0@"'.format(meson.current_build_dir()),
link_with : [ librpminspect ],
)

# Support program used by test-inspect-elf
execstack_prog = executable(
'execstack',
Expand All @@ -103,6 +113,7 @@ if cunit.found()
depends : [execstack_prog, noexecstack_prog]
)
test('test-abspath', test_abspath)
test('test-humansize', test_humansize)
else
warning('CUnit not found, skipping unit test suite')
endif
Expand Down

0 comments on commit 3d80445

Please sign in to comment.