commit: 6dee172499f83b50233873de2e9c5e54fc06c4bc
parent 033d7267b9e03bd871987f36ef136a0be6ce2597
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 16 Apr 2025 16:02:13 +0200
test-lib/t_humanize: new
Diffstat:
2 files changed, 71 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
@@ -32,7 +32,7 @@ selfcheck-cmds: $(EXE) $(TEST_CMDS)
LDSTATIC="$(LDSTATIC)" ./check-cmds.sh
.PHONY: selfcheck-libs
-TEST_LIBS = test-lib/t_mode test-lib/t_strtodur test-lib/t_symbolize_mode test-lib/t_truncation test-lib/t_sha1 test-lib/t_sha256 test-lib/t_sha512
+TEST_LIBS = test-lib/t_mode test-lib/t_strtodur test-lib/t_symbolize_mode test-lib/t_truncation test-lib/t_sha1 test-lib/t_sha256 test-lib/t_sha512 test-lib/t_humanize
selfcheck-libs: $(TEST_LIBS)
LDSTATIC="$(LDSTATIC)" ./check-libs.sh $(TEST_LIBS)
@@ -168,10 +168,11 @@ lib/sha256.o: lib/sha256.c lib/sha256.h
lib/sha512.o: lib/sha512.c lib/sha512.h
lib/bytes2hex.o: lib/bytes2hex.c lib/strconv.h
+test-lib/t_humanize: test-lib/t_humanize.c lib/humanize.o
test-lib/t_mode: test-lib/t_mode.c lib/mode.o
-test-lib/t_strtodur: test-lib/t_strtodur.c lib/strtodur.o
-test-lib/t_symbolize_mode: test-lib/t_symbolize_mode.c lib/symbolize_mode.o
-test-lib/t_truncation: test-lib/t_truncation.c lib/truncation.o
test-lib/t_sha1: test-lib/t_sha1.c lib/sha1.c lib/bytes2hex.o
test-lib/t_sha256: test-lib/t_sha256.c lib/sha256.c lib/bytes2hex.o
test-lib/t_sha512: test-lib/t_sha512.c lib/sha512.c lib/bytes2hex.o
+test-lib/t_strtodur: test-lib/t_strtodur.c lib/strtodur.o
+test-lib/t_symbolize_mode: test-lib/t_symbolize_mode.c lib/symbolize_mode.o
+test-lib/t_truncation: test-lib/t_truncation.c lib/truncation.o
diff --git a/test-lib/t_humanize.c b/test-lib/t_humanize.c
@@ -0,0 +1,66 @@
+// utils-std: Collection of commonly available Unix tools
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#define _POSIX_C_SOURCE 200809L
+#include "../lib/humanize.h"
+
+#include <assert.h>
+#include <stdio.h> // printf
+
+const char *argv0 = "t_humanize";
+int counter = 0;
+int err = 0;
+
+static void
+t_dtosi(double in, bool iec, double out, unsigned exponent)
+{
+ int id = ++counter;
+
+ struct si_scale ret = dtosi(in, iec);
+
+ if(ret.number == out && ret.exponent == exponent)
+ {
+ printf("ok %d - dtosi(%g, %d) => {.number = %g, .exponent = %d}\n",
+ id,
+ in,
+ iec,
+ ret.number,
+ ret.exponent);
+ return;
+ }
+
+ err = 1;
+ printf("not ok %d - dtosi(%g, %d) => {.number = %g, .exponent = %d}\n",
+ id,
+ in,
+ iec,
+ ret.number,
+ ret.exponent);
+ printf("# Expected: {.number = %g, .exponent = %d}\n", out, exponent);
+}
+
+int
+main(void)
+{
+ int plan = 10;
+ printf("1..%d\n", plan);
+
+ t_dtosi(0, false, 0, 0);
+ t_dtosi(0, true, 0, 0);
+
+ t_dtosi(1000, false, 1000, 0);
+ t_dtosi(10000, false, 10, 1);
+
+ t_dtosi(-1000, false, -1000, 0);
+ t_dtosi(-10000, false, -10, 1);
+
+ t_dtosi(1024, true, 1024, 0);
+ t_dtosi(10240, true, 10240 / 1024, 1);
+
+ t_dtosi(-1024, true, -1024, 0);
+ t_dtosi(-10240, true, -10240 / 1024, 1);
+
+ assert(counter == plan);
+ return err;
+}