Serenity Operating System
at master 135 lines 4.8 kB view raw
1/* 2 * Copyright (c) 2020, Ben Wiederhake <BenWiederhake.GitHub@gmx.de> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8 9#include <AK/NumberFormat.h> 10 11/* 12 * These tests are mostly meant as a rough sanity-check, to see whether 13 * human_readable_size() crashes or does something very silly. That, however, 14 * is a fuzzy human term, so these tests have to hard-code the exact expected 15 * strings. 16 * 17 * Please feel free to tweak human_readable_size()'s behavior, and update the 18 * "expected" strings below. 19 */ 20 21TEST_CASE(golden_path) 22{ 23 EXPECT_EQ(human_readable_size(0), "0 B"); 24 EXPECT_EQ(human_readable_size(123), "123 B"); 25 EXPECT_EQ(human_readable_size(123 * KiB), "123.0 KiB"); 26 EXPECT_EQ(human_readable_size(123 * MiB), "123.0 MiB"); 27 EXPECT_EQ(human_readable_size(2 * GiB), "2.0 GiB"); 28} 29 30TEST_CASE(border_B_KiB) 31{ 32 EXPECT_EQ(human_readable_size(1000), "1000 B"); 33 EXPECT_EQ(human_readable_size(1023), "1023 B"); 34 // KiB = 1024 35 EXPECT_EQ(human_readable_size(1024), "1.0 KiB"); 36 EXPECT_EQ(human_readable_size(1025), "1.0 KiB"); 37} 38 39TEST_CASE(fraction_KiB) 40{ 41 EXPECT_EQ(human_readable_size(1050), "1.0 KiB"); 42 EXPECT_EQ(human_readable_size(1075), "1.0 KiB"); 43 // 1024 * 1.05 = 1075.2 44 EXPECT_EQ(human_readable_size(1076), "1.0 KiB"); 45 46 EXPECT_EQ(human_readable_size(1100), "1.0 KiB"); 47 48 EXPECT_EQ(human_readable_size(1126), "1.0 KiB"); 49 // 1024 * 1.1 = 1126.4 50 EXPECT_EQ(human_readable_size(1127), "1.1 KiB"); 51 EXPECT_EQ(human_readable_size(1146), "1.1 KiB"); 52} 53 54TEST_CASE(border_KiB_MiB) 55{ 56 EXPECT_EQ(human_readable_size(1000 * KiB), "1000.0 KiB"); 57 EXPECT_EQ(human_readable_size(1024 * KiB - 1), "1023.9 KiB"); 58 // MiB 59 EXPECT_EQ(human_readable_size(1024 * KiB), "1.0 MiB"); 60 EXPECT_EQ(human_readable_size(1024 * KiB + 1), "1.0 MiB"); 61} 62 63TEST_CASE(fraction_MiB) 64{ 65 EXPECT_EQ(human_readable_size(1069547), "1.0 MiB"); 66 EXPECT_EQ(human_readable_size(1101004), "1.0 MiB"); 67 // 1024 * 1024 * 1.05 = 1101004.8 68 EXPECT_EQ(human_readable_size(1101005), "1.0 MiB"); 69 EXPECT_EQ(human_readable_size(1101006), "1.0 MiB"); 70 71 EXPECT_EQ(human_readable_size(1120000), "1.0 MiB"); 72 73 EXPECT_EQ(human_readable_size(1153433), "1.0 MiB"); 74 // 1024 * 1024 * 1.1 = 1153433.6 75 EXPECT_EQ(human_readable_size(1153434), "1.1 MiB"); 76} 77 78TEST_CASE(border_MiB_GiB) 79{ 80 EXPECT_EQ(human_readable_size(1000 * MiB), "1000.0 MiB"); 81 EXPECT_EQ(human_readable_size(1024 * MiB - 1), "1023.9 MiB"); 82 EXPECT_EQ(human_readable_size(1024 * MiB), "1.0 GiB"); 83 EXPECT_EQ(human_readable_size(1024 * MiB + 1), "1.0 GiB"); 84} 85 86TEST_CASE(fraction_GiB) 87{ 88 EXPECT_EQ(human_readable_size(1095216660), "1.0 GiB"); 89 EXPECT_EQ(human_readable_size(1127428915), "1.0 GiB"); 90 // 1024 * 1024 * 1024 * 1.05 = 1127428915.2 91 EXPECT_EQ(human_readable_size(1127428916), "1.0 GiB"); 92 EXPECT_EQ(human_readable_size(1127536289), "1.0 GiB"); 93 94 EXPECT_EQ(human_readable_size(1154272461), "1.0 GiB"); 95 96 EXPECT_EQ(human_readable_size(1181115968), "1.0 GiB"); 97 EXPECT_EQ(human_readable_size(1181115969), "1.0 GiB"); 98 EXPECT_EQ(human_readable_size(1181116000), "1.0 GiB"); 99 EXPECT_EQ(human_readable_size(1181116006), "1.0 GiB"); 100 // 1024 * 1024 * 1024 * 1.1 = 1181116006.4 101 EXPECT_EQ(human_readable_size(1181116007), "1.1 GiB"); 102 EXPECT_EQ(human_readable_size(1202590842), "1.1 GiB"); 103} 104 105TEST_CASE(extremes_4byte) 106{ 107 EXPECT_EQ(human_readable_size(0x7fffffff), "1.9 GiB"); 108 EXPECT_EQ(human_readable_size(0x80000000), "2.0 GiB"); 109 EXPECT_EQ(human_readable_size(0xffffffff), "3.9 GiB"); 110} 111 112TEST_CASE(extremes_8byte) 113{ 114 if constexpr (sizeof(size_t) == 8) { 115 warnln("(Running 8-byte-size_t test)"); 116 EXPECT_EQ(human_readable_size(0x100000000ULL), "4.0 GiB"); 117 EXPECT_EQ(human_readable_size(0x100000001ULL), "4.0 GiB"); 118 EXPECT_EQ(human_readable_size(0x800000000ULL), "32.0 GiB"); 119 EXPECT_EQ(human_readable_size(0x10000000000ULL), "1.0 TiB"); 120 EXPECT_EQ(human_readable_size(0x4000000000000ULL), "1.0 PiB"); 121 EXPECT_EQ(human_readable_size(0x7fffffffffffffffULL), "7.9 EiB"); 122 EXPECT_EQ(human_readable_size(0x8000000000000000ULL), "8.0 EiB"); 123 EXPECT_EQ(human_readable_size(0xffffffffffffffffULL), "15.9 EiB"); 124 } else { 125 warnln("(Skipping 8-byte-size_t test on 32-bit platform)"); 126 } 127} 128 129TEST_CASE(base10_units) 130{ 131 EXPECT_EQ(human_readable_size(999, AK::HumanReadableBasedOn::Base10), "999 B"); 132 EXPECT_EQ(human_readable_size(1024, AK::HumanReadableBasedOn::Base10), "1.0 KB"); 133 EXPECT_EQ(human_readable_size(1100, AK::HumanReadableBasedOn::Base10), "1.1 KB"); 134 EXPECT_EQ(human_readable_size(1000000, AK::HumanReadableBasedOn::Base10), "1.0 MB"); 135}