View Javadoc
1   /*
2    * Copyright 2016-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.is;
9   
10  import java.text.DecimalFormatSymbols;
11  import java.util.Locale;
12  
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * The Class FormatUtilTest.
17   */
18  class FormatUtilTest {
19  
20      private static final char DECIMAL_SEPARATOR = DecimalFormatSymbols.getInstance(Locale.ROOT).getDecimalSeparator();
21  
22      /**
23       * Test format bytes.
24       */
25      @Test
26      void testFormatBytes() {
27          assertThat("format 0 bytes", FormatUtil.formatBytes(0), is("0 bytes"));
28          assertThat("format byte", FormatUtil.formatBytes(1), is("1 byte"));
29          assertThat("format bytes", FormatUtil.formatBytes(532), is("532 bytes"));
30          assertThat("format KiByte", FormatUtil.formatBytes(1024), is("1 KiB"));
31          assertThat("format GiByte", FormatUtil.formatBytes(1024 * 1024 * 1024), is("1 GiB"));
32          assertThat("format TiByte", FormatUtil.formatBytes(1099511627776L), is("1 TiB"));
33      }
34  
35      /**
36       * Test format bytes with decimal separator.
37       */
38      @Test
39      void testFormatBytesWithDecimalSeparator() {
40          String expected1 = "1" + DECIMAL_SEPARATOR + "3 KiB";
41          String expected2 = "2" + DECIMAL_SEPARATOR + "3 MiB";
42          String expected3 = "2" + DECIMAL_SEPARATOR + "2 GiB";
43          String expected4 = "1" + DECIMAL_SEPARATOR + "1 TiB";
44          String expected5 = "1" + DECIMAL_SEPARATOR + "1 PiB";
45          String expected6 = "1" + DECIMAL_SEPARATOR + "1 EiB";
46          assertThat("format KiBytes with decimal separator", FormatUtil.formatBytes(1340), is(expected1));
47          assertThat("format MiBytes with decimal separator", FormatUtil.formatBytes(2400016), is(expected2));
48          assertThat("format GiBytes with decimal separator", FormatUtil.formatBytes(2400000000L), is(expected3));
49          assertThat("format TiBytes with decimal separator", FormatUtil.formatBytes(1099511627776L + 109951162777L),
50                  is(expected4));
51          assertThat("format PiBytes with decimal separator",
52                  FormatUtil.formatBytes(1125899906842624L + 112589990684262L), is(expected5));
53          assertThat("format EiBytes with decimal separator",
54                  FormatUtil.formatBytes(1152921504606846976L + 115292150460684698L), is(expected6));
55      }
56  
57      /**
58       * Test format decimal bytes.
59       */
60      @Test
61      void testFormatBytesDecimal() {
62          assertThat("format 0 bytesDecimal", FormatUtil.formatBytesDecimal(0), is("0 bytes"));
63          assertThat("format byteDecimal", FormatUtil.formatBytesDecimal(1), is("1 byte"));
64          assertThat("format bytesDecimal", FormatUtil.formatBytesDecimal(532), is("532 bytes"));
65          assertThat("format KbytesDecimal", FormatUtil.formatBytesDecimal(1000), is("1 KB"));
66          assertThat("format GbytesDecimal", FormatUtil.formatBytesDecimal(1000 * 1000 * 1000), is("1 GB"));
67          assertThat("format TbytesDecimal", FormatUtil.formatBytesDecimal(1000000000000L), is("1 TB"));
68      }
69  
70      /**
71       * Test format decimal bytes with decimal separator.
72       */
73      @Test
74      void testFormatBytesDecimalWithDecimalSeparator() {
75          String expected1 = "1" + DECIMAL_SEPARATOR + "3 KB";
76          String expected2 = "2" + DECIMAL_SEPARATOR + "3 MB";
77          String expected3 = "2" + DECIMAL_SEPARATOR + "2 GB";
78          String expected4 = "1" + DECIMAL_SEPARATOR + "1 TB";
79          String expected5 = "3" + DECIMAL_SEPARATOR + "4 PB";
80          String expected6 = "5" + DECIMAL_SEPARATOR + "6 EB";
81          assertThat("format KBytes with decimal separator", FormatUtil.formatBytesDecimal(1300), is(expected1));
82          assertThat("format MBytes with decimal separator", FormatUtil.formatBytesDecimal(2300000), is(expected2));
83          assertThat("format GBytes with decimal separator", FormatUtil.formatBytesDecimal(2200000000L), is(expected3));
84          assertThat("format TBytes with decimal separator", FormatUtil.formatBytesDecimal(1100000000000L),
85                  is(expected4));
86          assertThat("format PBytes with decimal separator", FormatUtil.formatBytesDecimal(3400000000000000L),
87                  is(expected5));
88          assertThat("format EBytes with decimal separator", FormatUtil.formatBytesDecimal(5600000000000000000L),
89                  is(expected6));
90      }
91  
92      /**
93       * Test format hertz.
94       */
95      @Test
96      void testFormatHertz() {
97          assertThat("format zero Hertz", FormatUtil.formatHertz(0), is("0 Hz"));
98          assertThat("format one Hertz", FormatUtil.formatHertz(1), is("1 Hz"));
99          assertThat("format many Hertz", FormatUtil.formatHertz(999), is("999 Hz"));
100         assertThat("format KHertz", FormatUtil.formatHertz(1000), is("1 KHz"));
101         assertThat("format MHertz", FormatUtil.formatHertz(1000 * 1000), is("1 MHz"));
102         assertThat("format GHertz", FormatUtil.formatHertz(1000 * 1000 * 1000), is("1 GHz"));
103         assertThat("format THertz", FormatUtil.formatHertz(1000L * 1000L * 1000L * 1000L), is("1 THz"));
104     }
105 
106     /**
107      * Test format elapsed secs
108      */
109     @Test
110     void testFormatElapsedSecs() {
111         assertThat("format 0 elapsed seconds", FormatUtil.formatElapsedSecs(0), is("0 days, 00:00:00"));
112         assertThat("format many elapsed seconds", FormatUtil.formatElapsedSecs(12345), is("0 days, 03:25:45"));
113         assertThat("format elapsed day in seconds", FormatUtil.formatElapsedSecs(123456), is("1 days, 10:17:36"));
114         assertThat("format elapsed days in seconds", FormatUtil.formatElapsedSecs(1234567), is("14 days, 06:56:07"));
115     }
116 
117     /**
118      * Test unsigned int to long.
119      */
120     @Test
121     void testGetUnsignedInt() {
122         assertThat("unsigned int", FormatUtil.getUnsignedInt(-1), is(4294967295L));
123     }
124 
125     /**
126      * Test unsigned string
127      */
128     @Test
129     void testToUnsignedString() {
130         assertThat("Integer to unsigned string", FormatUtil.toUnsignedString(0x00000001), is("1"));
131         assertThat("Big Integer to unsigned string", FormatUtil.toUnsignedString(0x80000000), is("2147483648"));
132         assertThat("INT_MAX to unsigned string", FormatUtil.toUnsignedString(0xffffffff), is("4294967295"));
133 
134         assertThat("Long to unsigned string", FormatUtil.toUnsignedString(0x0000000000000001L), is("1"));
135         assertThat("Big Long to unsigned string", FormatUtil.toUnsignedString(0x8000000000000000L),
136                 is("9223372036854775808"));
137         assertThat("LONG_MAX to unsigned string", FormatUtil.toUnsignedString(0xffffffffffffffffL),
138                 is("18446744073709551615"));
139     }
140 
141     /**
142      * Test format error
143      */
144     @Test
145     void testFormatError() {
146         assertThat("Format error code", FormatUtil.formatError(-1234567000), is("0xB66A00A8"));
147     }
148 
149     /**
150      * Test round to int
151      */
152     @Test
153     void testRoundToInt() {
154         assertThat("Improper rounding pi", FormatUtil.roundToInt(Math.PI), is(3));
155         assertThat("Improper rounding e", FormatUtil.roundToInt(Math.E), is(3));
156         assertThat("Improper rounding 0", FormatUtil.roundToInt(0d), is(0));
157         assertThat("Improper rounding 1", FormatUtil.roundToInt(1d), is(1));
158     }
159 
160 }