From 911c5f2661808dcb4d4292cdd0eed424509f118a Mon Sep 17 00:00:00 2001 From: David Korth Date: Wed, 28 Jun 2023 22:06:15 -0400 Subject: [PATCH] [librptext/tests] TextFuncsTest: Add tests for formatFrequency(). Unlike the file size functions, formatFrequency() works on base 10, and always has three decimal places once it gets into the kHz range. librptext code coverage: Before: lines......: 86.7% (424 of 489 lines) functions..: 86.5% (32 of 37 functions) After: lines......: 88.5% (433 of 489 lines) functions..: 86.5% (32 of 37 functions) Overall code coverage: Before: lines......: 22.2% (11580 of 52107 lines) functions..: 21.3% (773 of 3625 functions) After: lines......: 22.2% (11589 of 52107 lines) functions..: 21.3% (773 of 3625 functions) --- src/librptext/tests/TextFuncsTest.cpp | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/librptext/tests/TextFuncsTest.cpp b/src/librptext/tests/TextFuncsTest.cpp index 6459ddc81..ceed545f4 100644 --- a/src/librptext/tests/TextFuncsTest.cpp +++ b/src/librptext/tests/TextFuncsTest.cpp @@ -1054,6 +1054,51 @@ TEST_F(TextFuncsTest, formatFileSizeKiB) EXPECT_EQ("4194303 KiB", formatFileSizeKiB(0xFFFFFFFFU)); } +/** + * Test formatFrequency(). + */ +TEST_F(TextFuncsTest, formatFrequency) +{ + // Testing 512, 768, 1024, 1536, 2048, 2560, and 3072 for each order of magnitude. + // NOTE: Frequencies aren't powers of two, so the resulting values will + // have "weird" decimal points. + + // Special cases + EXPECT_EQ("0 Hz", formatFrequency(0U)); + EXPECT_EQ("1 Hz", formatFrequency(1U)); + EXPECT_EQ("2 Hz", formatFrequency(2U)); + + // Kilohertz + EXPECT_EQ("512 Hz", formatFrequency(512U)); + EXPECT_EQ("768 Hz", formatFrequency(768U)); + EXPECT_EQ("1024 Hz", formatFrequency(1024U)); + EXPECT_EQ("1536 Hz", formatFrequency(1536U)); + EXPECT_EQ("2.048 kHz", formatFrequency(2048U)); + EXPECT_EQ("2.560 kHz", formatFrequency(2560U)); + EXPECT_EQ("3.072 kHz", formatFrequency(3072U)); + + // Megahertz + EXPECT_EQ("524.288 kHz", formatFrequency(512U*1024U)); + EXPECT_EQ("786.432 kHz", formatFrequency(768U*1024U)); + EXPECT_EQ("1048.576 kHz", formatFrequency(1024U*1024U)); + EXPECT_EQ("1572.864 kHz", formatFrequency(1536U*1024U)); + EXPECT_EQ("2.097 MHz", formatFrequency(2048U*1024U)); + EXPECT_EQ("2.621 MHz", formatFrequency(2560U*1024U)); + EXPECT_EQ("3.145 MHz", formatFrequency(3072U*1024U)); + + // Gigabyte + EXPECT_EQ("536.870 MHz", formatFrequency(512U*1024U*1024U)); + EXPECT_EQ("805.306 MHz", formatFrequency(768U*1024U*1024U)); + EXPECT_EQ("1073.741 MHz", formatFrequency(1024U*1024U*1024U)); + EXPECT_EQ("1610.612 MHz", formatFrequency(1536U*1024U*1024U)); + EXPECT_EQ("2.147 GHz", formatFrequency(2048U*1024U*1024U)); + EXPECT_EQ("2.684 GHz", formatFrequency(2560U*1024U*1024U)); + EXPECT_EQ("3.221 GHz", formatFrequency(3072U*1024U*1024U)); + + // Largest value for a 32-bit unsigned integer + EXPECT_EQ("4.294 GHz", formatFrequency(0xFFFFFFFFU)); +} + /** * Test dos2unix(). */