Skip to content

Commit

Permalink
Merge pull request systemd#33081 from keszybz/networkctl-formatting
Browse files Browse the repository at this point in the history
Fix formatting of speeds in networkctl
  • Loading branch information
bluca authored May 30, 2024
2 parents 6ecdd5e + 12080b1 commit 0630069
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 20 deletions.
12 changes: 7 additions & 5 deletions src/basic/format-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {

for (size_t i = 0; i < n; i++)
if (t >= table[i].factor) {
if (flag & FORMAT_BYTES_BELOW_POINT) {
uint64_t remainder = i != n - 1 ?
(t / table[i + 1].factor * 10 / table[n - 1].factor) % 10 :
(t * 10 / table[i].factor) % 10;

if (FLAGS_SET(flag, FORMAT_BYTES_BELOW_POINT) && remainder > 0)
(void) snprintf(buf, l,
"%" PRIu64 ".%" PRIu64 "%s",
t / table[i].factor,
i != n - 1 ?
(t / table[i + 1].factor * UINT64_C(10) / table[n - 1].factor) % UINT64_C(10):
(t * UINT64_C(10) / table[i].factor) % UINT64_C(10),
remainder,
table[i].suffix);
} else
else
(void) snprintf(buf, l,
"%" PRIu64 "%s",
t / table[i].factor,
Expand Down
2 changes: 1 addition & 1 deletion src/shared/format-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
if (!p)
return NULL;

if (!format_bytes_full(p, FORMAT_BYTES_MAX, d->size, 0))
if (!format_bytes_full(p, FORMAT_BYTES_MAX, d->size, FORMAT_BYTES_BELOW_POINT))
return table_ersatz_string(t);

n = strlen(p);
Expand Down
37 changes: 36 additions & 1 deletion src/test/test-format-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ TEST(vertical) {

assert_se(streq(formatted,
" pfft aa: foo\n"
" uuu o: 1.0K\n"
" uuu o: 1K\n"
"lllllllllllo: jjjjjjjjjjjjjjjjj\n"));

_cleanup_(json_variant_unrefp) JsonVariant *a = NULL, *b = NULL;
Expand Down Expand Up @@ -626,6 +626,41 @@ TEST(dup_cell) {
"aaa 0 65535 4294967295 100% ../ hello hello hello\n"));
}

TEST(table_bps) {
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_free_ char *formatted = NULL;

assert_se(table = table_new("uint64", "size", "bps"));
uint64_t v;
FOREACH_ARGUMENT(v,
2500,
10000000,
20000000,
25000000,
1000000000,
2000000000,
2500000000)
assert_se(table_add_many(table,
TABLE_UINT64, v,
TABLE_SIZE, v,
TABLE_BPS, v) >= 0);

table_set_width(table, 50);
assert_se(table_format(table, &formatted) >= 0);

printf("%s", formatted);
assert_se(streq(formatted,
"UINT64 SIZE BPS\n"
"2500 2.4K 2.5Kbps\n"
"10000000 9.5M 10Mbps\n"
"20000000 19M 20Mbps\n"
"25000000 23.8M 25Mbps\n"
"1000000000 953.6M 1Gbps\n"
"2000000000 1.8G 2Gbps\n"
"2500000000 2.3G 2.5Gbps\n"
));
}

static int intro(void) {
assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
assert_se(setenv("COLUMNS", "40", 1) >= 0);
Expand Down
20 changes: 10 additions & 10 deletions src/test/test-format-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ static void test_format_bytes_one(uint64_t val, bool trailing_B, const char *iec
TEST(format_bytes) {
test_format_bytes_one(900, true, "900B", "900B", "900B", "900B");
test_format_bytes_one(900, false, "900", "900", "900", "900");
test_format_bytes_one(1023, true, "1023B", "1023B", "1.0K", "1K");
test_format_bytes_one(1023, false, "1023", "1023", "1.0K", "1K");
test_format_bytes_one(1024, true, "1.0K", "1K", "1.0K", "1K");
test_format_bytes_one(1024, false, "1.0K", "1K", "1.0K", "1K");
test_format_bytes_one(1100, true, "1.0K", "1K", "1.1K", "1K");
test_format_bytes_one(1023, true, "1023B", "1023B", "1K", "1K");
test_format_bytes_one(1023, false, "1023", "1023", "1K", "1K");
test_format_bytes_one(1024, true, "1K", "1K", "1K", "1K");
test_format_bytes_one(1024, false, "1K", "1K", "1K", "1K");
test_format_bytes_one(1100, true, "1K", "1K", "1.1K", "1K");
test_format_bytes_one(1500, true, "1.4K", "1K", "1.5K", "1K");
test_format_bytes_one(UINT64_C(3)*1024*1024, true, "3.0M", "3M", "3.1M", "3M");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024, true, "3.0G", "3G", "3.2G", "3G");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024, true, "3.0T", "3T", "3.2T", "3T");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024, true, "3.0P", "3P", "3.3P", "3P");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024*1024, true, "3.0E", "3E", "3.4E", "3E");
test_format_bytes_one(UINT64_C(3)*1024*1024, true, "3M", "3M", "3.1M", "3M");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024, true, "3G", "3G", "3.2G", "3G");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024, true, "3T", "3T", "3.2T", "3T");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024, true, "3P", "3P", "3.3P", "3P");
test_format_bytes_one(UINT64_C(3)*1024*1024*1024*1024*1024*1024, true, "3E", "3E", "3.4E", "3E");
test_format_bytes_one(UINT64_MAX, true, NULL, NULL, NULL, NULL);
test_format_bytes_one(UINT64_MAX, false, NULL, NULL, NULL, NULL);
}
Expand Down
6 changes: 3 additions & 3 deletions test/units/TEST-58-REPART.sh
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ EOF
"offset" : 1048576,
"old_size" : 0,
"raw_size" : 33554432,
"size" : "-> 32.0M",
"size" : "-> 32M",
"old_padding" : 0,
"raw_padding" : 0,
"padding" : "-> 0B",
Expand Down Expand Up @@ -496,7 +496,7 @@ EOF
"offset" : 1048576,
"old_size" : 0,
"raw_size" : 33554432,
"size" : "-> 32.0M",
"size" : "-> 32M",
"old_padding" : 0,
"raw_padding" : 0,
"padding" : "-> 0B",
Expand All @@ -512,7 +512,7 @@ EOF
"offset" : 34603008,
"old_size" : 0,
"raw_size" : 33554432,
"size" : "-> 32.0M",
"size" : "-> 32M",
"old_padding" : 0,
"raw_padding" : 0,
"padding" : "-> 0B",
Expand Down

0 comments on commit 0630069

Please sign in to comment.