8083fa54cf
decimal and binary conversion (http://en.wikipedia.org/wiki/Megabyte)
114 lines
2.9 KiB
C
114 lines
2.9 KiB
C
#include <check.h>
|
|
#include <ucore/string.h>
|
|
|
|
|
|
START_TEST (test_human_bytesz_bytes)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(999, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "999 b");
|
|
|
|
rc = uc_human_bytesz_bin(999, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "999 b");
|
|
END_TEST
|
|
|
|
START_TEST (test_human_bytesz_bytes_0)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(0, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "0 b");
|
|
|
|
rc = uc_human_bytesz_bin(0, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "0 b");
|
|
END_TEST
|
|
|
|
START_TEST (test_human_bytesz_kilobytes)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(64000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "64.00 kB");
|
|
|
|
rc = uc_human_bytesz_bin(64000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "62.50 KiB");
|
|
END_TEST
|
|
|
|
START_TEST (test_human_bytesz_megabytes)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(6409000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "6.41 MB");
|
|
|
|
rc = uc_human_bytesz_bin(6409000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "6.11 MiB");
|
|
END_TEST
|
|
|
|
START_TEST (test_human_bytesz_gigabytes)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(126999400000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "127.00 GB");
|
|
|
|
rc = uc_human_bytesz_bin(126999400000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "118.28 GiB");
|
|
END_TEST
|
|
|
|
START_TEST (test_human_bytesz_terrabytes)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(12453400030000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "12.45 TB");
|
|
|
|
rc = uc_human_bytesz_bin(12453400030000, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "11.33 TiB");
|
|
END_TEST
|
|
|
|
START_TEST (test_human_bytesz_petabytes)
|
|
char result[128];
|
|
char *rc;
|
|
|
|
rc = uc_human_bytesz_dec(~0LL, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "18446.74 PB");
|
|
|
|
rc = uc_human_bytesz_bin(~0ULL, result, sizeof result);
|
|
fail_if(rc == NULL);
|
|
ck_assert_str_eq(result, "16384.00 PiB");
|
|
END_TEST
|
|
|
|
|
|
Suite *human_bytesz_suite(void)
|
|
{
|
|
Suite *s = suite_create("human_bytesz");
|
|
TCase *tc = tcase_create("human_bytesz tests");
|
|
tcase_add_test(tc, test_human_bytesz_bytes);
|
|
tcase_add_test(tc, test_human_bytesz_bytes_0);
|
|
tcase_add_test(tc, test_human_bytesz_kilobytes);
|
|
tcase_add_test(tc, test_human_bytesz_megabytes);
|
|
tcase_add_test(tc, test_human_bytesz_gigabytes);
|
|
tcase_add_test(tc, test_human_bytesz_terrabytes);
|
|
tcase_add_test(tc, test_human_bytesz_petabytes);
|
|
|
|
suite_add_tcase(s, tc);
|
|
|
|
return s;
|
|
}
|
|
|