View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import java.util.Locale;
14  
15  import org.junit.jupiter.api.AfterEach;
16  import org.junit.jupiter.api.Assertions;
17  import org.junit.jupiter.api.BeforeEach;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * The Class SizeExpressionTests.
22   */
23  class SizeExpressionTests {
24  
25    /** The default locale. */
26    private Locale defaultLocale;
27  
28    /**
29     * Sets the up.
30     */
31    @BeforeEach
32    void setUp() {
33      this.defaultLocale = Locale.getDefault();
34      Locale.setDefault(Locale.US);
35    }
36  
37    /**
38     * Tear down.
39     */
40    @AfterEach
41    void tearDown() {
42      Locale.setDefault(defaultLocale);
43    }
44  
45    /**
46     * Test format no decimal base2.
47     */
48    @Test
49    void formatNoDecimalBase2Test() {
50      Assertions.assertEquals("1 B", SizeExpression.format(1, 0, true));
51      Assertions.assertEquals("10 B", SizeExpression.format(10, 0, true));
52      Assertions.assertEquals("100 B", SizeExpression.format(100, 0, true));
53      Assertions.assertEquals("1,000 B", SizeExpression.format(1000, 0, true));
54      Assertions.assertEquals("1,023 B", SizeExpression.format(1023, 0, true));
55      Assertions.assertEquals("1 KB", SizeExpression.format(1024, 0, true));
56      Assertions.assertEquals("10 KB", SizeExpression.format(10240, 0, true));
57      Assertions.assertEquals("10 KB", SizeExpression.format(10250, 0, true));
58    }
59  
60    /**
61     * Test format no decimal base10.
62     */
63    @Test
64    void formatNoDecimalBase10Test() {
65      Assertions.assertEquals("1", SizeExpression.format(1, 0, false));
66      Assertions.assertEquals("10", SizeExpression.format(10, 0, false));
67      Assertions.assertEquals("100", SizeExpression.format(100, 0, false));
68      Assertions.assertEquals("1K", SizeExpression.format(1000, 0, false));
69      Assertions.assertEquals("1K", SizeExpression.format(1023, 0, false));
70      Assertions.assertEquals("1K", SizeExpression.format(1024, 0, false));
71      Assertions.assertEquals("10K", SizeExpression.format(10240, 0, false));
72      Assertions.assertEquals("10K", SizeExpression.format(10250, 0, false));
73    }
74  
75    /**
76     * Test format one decimal base2.
77     */
78    @Test
79    void formatOneDecimalBase2Test() {
80      Assertions.assertEquals("1 B", SizeExpression.format(1, 1, true));
81      Assertions.assertEquals("10 B", SizeExpression.format(10, 1, true));
82      Assertions.assertEquals("100 B", SizeExpression.format(100, 1, true));
83      Assertions.assertEquals("1,000 B", SizeExpression.format(1000, 1, true));
84      Assertions.assertEquals("1,023 B", SizeExpression.format(1023, 1, true));
85      Assertions.assertEquals("1.0 KB", SizeExpression.format(1024, 1, true));
86      Assertions.assertEquals("10.0 KB", SizeExpression.format(10240, 1, true));
87      Assertions.assertEquals("10.0 KB", SizeExpression.format(10250, 1, true));
88    }
89  
90    /**
91     * Test format one decimal base10.
92     */
93    @Test
94    void formatOneDecimalBase10Test() {
95      Assertions.assertEquals("1", SizeExpression.format(1, 1, false));
96      Assertions.assertEquals("10", SizeExpression.format(10, 1, false));
97      Assertions.assertEquals("100", SizeExpression.format(100, 1, false));
98      Assertions.assertEquals("1.0K", SizeExpression.format(1000, 1, false));
99      Assertions.assertEquals("1.0K", SizeExpression.format(1023, 1, false));
100     Assertions.assertEquals("1.0K", SizeExpression.format(1024, 1, false));
101     Assertions.assertEquals("10.2K", SizeExpression.format(10240, 1, false));
102     Assertions.assertEquals("10.3K", SizeExpression.format(10250, 1, false));
103   }
104 
105   /**
106    * Test format all prefixes base2.
107    */
108   @Test
109   void formatAllPrefixesBase2Test() {
110     Assertions.assertEquals("1 B", SizeExpression.format(1, 0, true));
111     Assertions.assertEquals("1 KB", SizeExpression.format(1024, 0, true));
112     Assertions.assertEquals("1 MB", SizeExpression.format(1048576, 0, true));
113     Assertions.assertEquals("1 GB", SizeExpression.format(1073741824, 0, true));
114     Assertions.assertEquals("1 TB", SizeExpression.format(1099511627776L, 0, true));
115     Assertions.assertEquals("1 PB", SizeExpression.format(1125899906842624L, 0, true));
116   }
117 
118   /**
119    * Test format all prefixes base10.
120    */
121   @Test
122   void formatAllPrefixesBase10Test() {
123     Assertions.assertEquals("1", SizeExpression.format(1, 0, false));
124     Assertions.assertEquals("1K", SizeExpression.format(1000, 0, false));
125     Assertions.assertEquals("1M", SizeExpression.format(1000000, 0, false));
126     Assertions.assertEquals("1G", SizeExpression.format(1000000000, 0, false));
127     Assertions.assertEquals("1T", SizeExpression.format(1000000000000L, 0, false));
128     Assertions.assertEquals("1P", SizeExpression.format(1000000000000000L, 0, false));
129   }
130 
131   /**
132    * Test parse with unit.
133    */
134   @Test
135   void parseWithUnitTest() {
136     Assertions.assertEquals(1, SizeExpression.parse("1B"));
137     Assertions.assertEquals(10, SizeExpression.parse("10B"));
138     Assertions.assertEquals(100, SizeExpression.parse("100B"));
139     Assertions.assertEquals(1000, SizeExpression.parse("1000B"));
140     Assertions.assertEquals(1023, SizeExpression.parse("1023B"));
141     Assertions.assertEquals(1024, SizeExpression.parse("1024B"));
142     Assertions.assertEquals(1024, SizeExpression.parse("1.0KB"));
143     Assertions.assertEquals(1024, SizeExpression.parse("1KB"));
144     Assertions.assertEquals(1048576, SizeExpression.parse("1MB"));
145     Assertions.assertEquals(1073741824, SizeExpression.parse("1GB"));
146     Assertions.assertEquals(1099511627776L, SizeExpression.parse("1TB"));
147     Assertions.assertEquals(1125899906842624L, SizeExpression.parse("1PB"));
148   }
149 
150   /**
151    * Test parse without unit.
152    */
153   @Test
154   void parseWithoutUnitTest() {
155     Assertions.assertEquals(1, SizeExpression.parse("1"));
156     Assertions.assertEquals(10, SizeExpression.parse("10"));
157     Assertions.assertEquals(100, SizeExpression.parse("100"));
158     Assertions.assertEquals(1000, SizeExpression.parse("1000"));
159     Assertions.assertEquals(1023, SizeExpression.parse("1023"));
160     Assertions.assertEquals(1024, SizeExpression.parse("1024"));
161     Assertions.assertEquals(1000, SizeExpression.parse("1.0K"));
162     Assertions.assertEquals(1000, SizeExpression.parse("1K"));
163     Assertions.assertEquals(1000000, SizeExpression.parse("1M"));
164     Assertions.assertEquals(1000000000, SizeExpression.parse("1G"));
165     Assertions.assertEquals(1000000000000L, SizeExpression.parse("1T"));
166     Assertions.assertEquals(1000000000000000L, SizeExpression.parse("1P"));
167   }
168 
169 }