1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.luni.tests.java.lang;
18
19import junit.framework.TestCase;
20
21public class ByteTest extends TestCase {
22
23    /**
24     * @tests java.lang.Byte#valueOf(byte)
25     */
26    public void test_valueOfB() {
27        assertEquals(new Byte(Byte.MIN_VALUE), Byte.valueOf(Byte.MIN_VALUE));
28        assertEquals(new Byte(Byte.MAX_VALUE), Byte.valueOf(Byte.MAX_VALUE));
29        assertEquals(new Byte((byte) 0), Byte.valueOf((byte) 0));
30
31        byte b = Byte.MIN_VALUE + 1;
32        while (b < Byte.MAX_VALUE) {
33            assertEquals(new Byte(b), Byte.valueOf(b));
34            assertSame(Byte.valueOf(b), Byte.valueOf(b));
35            b++;
36        }
37    }
38
39    /**
40     * @tests java.lang.Byte#hashCode()
41     */
42    public void test_hashCode() {
43        assertEquals(1, new Byte((byte) 1).hashCode());
44        assertEquals(2, new Byte((byte) 2).hashCode());
45        assertEquals(0, new Byte((byte) 0).hashCode());
46        assertEquals(-1, new Byte((byte) -1).hashCode());
47    }
48
49    /**
50     * @tests java.lang.Byte#Byte(String)
51     */
52    public void test_ConstructorLjava_lang_String() {
53        assertEquals(new Byte((byte) 0), new Byte("0"));
54        assertEquals(new Byte((byte) 1), new Byte("1"));
55        assertEquals(new Byte((byte) -1), new Byte("-1"));
56
57        try {
58            new Byte("0x1");
59            fail("Expected NumberFormatException with hex string.");
60        } catch (NumberFormatException e) {
61        }
62
63        try {
64            new Byte("9.2");
65            fail("Expected NumberFormatException with floating point string.");
66        } catch (NumberFormatException e) {
67        }
68
69        try {
70            new Byte("");
71            fail("Expected NumberFormatException with empty string.");
72        } catch (NumberFormatException e) {
73        }
74
75        try {
76            new Byte(null);
77            fail("Expected NumberFormatException with null string.");
78        } catch (NumberFormatException e) {
79        }
80    }
81
82    /**
83     * @tests java.lang.Byte#Byte(byte)
84     */
85    public void test_ConstructorB() {
86        assertEquals(1, new Byte((byte) 1).byteValue());
87        assertEquals(2, new Byte((byte) 2).byteValue());
88        assertEquals(0, new Byte((byte) 0).byteValue());
89        assertEquals(-1, new Byte((byte) -1).byteValue());
90    }
91
92    /**
93     * @tests java.lang.Byte#byteValue()
94     */
95    public void test_booleanValue() {
96        assertEquals(1, new Byte((byte) 1).byteValue());
97        assertEquals(2, new Byte((byte) 2).byteValue());
98        assertEquals(0, new Byte((byte) 0).byteValue());
99        assertEquals(-1, new Byte((byte) -1).byteValue());
100    }
101
102    /**
103     * @tests java.lang.Byte#equals(Object)
104     */
105    public void test_equalsLjava_lang_Object() {
106        assertEquals(new Byte((byte) 0), Byte.valueOf((byte) 0));
107        assertEquals(new Byte((byte) 1), Byte.valueOf((byte) 1));
108        assertEquals(new Byte((byte) -1), Byte.valueOf((byte) -1));
109
110        Byte fixture = new Byte((byte) 25);
111        assertEquals(fixture, fixture);
112        assertFalse(fixture.equals(null));
113        assertFalse(fixture.equals("Not a Byte"));
114    }
115
116    /**
117     * @tests java.lang.Byte#toString()
118     */
119    public void test_toString() {
120        assertEquals("-1", new Byte((byte) -1).toString());
121        assertEquals("0", new Byte((byte) 0).toString());
122        assertEquals("1", new Byte((byte) 1).toString());
123        assertEquals("-1", new Byte((byte) 0xFF).toString());
124    }
125
126    /**
127     * @tests java.lang.Byte#toString(byte)
128     */
129    public void test_toStringB() {
130        assertEquals("-1", Byte.toString((byte) -1));
131        assertEquals("0", Byte.toString((byte) 0));
132        assertEquals("1", Byte.toString((byte) 1));
133        assertEquals("-1", Byte.toString((byte) 0xFF));
134    }
135
136    /**
137     * @tests java.lang.Byte#valueOf(String)
138     */
139    public void test_valueOfLjava_lang_String() {
140        assertEquals(new Byte((byte) 0), Byte.valueOf("0"));
141        assertEquals(new Byte((byte) 1), Byte.valueOf("1"));
142        assertEquals(new Byte((byte) -1), Byte.valueOf("-1"));
143
144        try {
145            Byte.valueOf("0x1");
146            fail("Expected NumberFormatException with hex string.");
147        } catch (NumberFormatException e) {
148        }
149
150        try {
151            Byte.valueOf("9.2");
152            fail("Expected NumberFormatException with floating point string.");
153        } catch (NumberFormatException e) {
154        }
155
156        try {
157            Byte.valueOf("");
158            fail("Expected NumberFormatException with empty string.");
159        } catch (NumberFormatException e) {
160        }
161
162        try {
163            Byte.valueOf(null);
164            fail("Expected NumberFormatException with null string.");
165        } catch (NumberFormatException e) {
166        }
167    }
168
169    /**
170     * @tests java.lang.Byte#valueOf(String,int)
171     */
172    public void test_valueOfLjava_lang_StringI() {
173        assertEquals(new Byte((byte) 0), Byte.valueOf("0", 10));
174        assertEquals(new Byte((byte) 1), Byte.valueOf("1", 10));
175        assertEquals(new Byte((byte) -1), Byte.valueOf("-1", 10));
176
177        //must be consistent with Character.digit()
178        assertEquals(Character.digit('1', 2), Byte.valueOf("1", 2).byteValue());
179        assertEquals(Character.digit('F', 16), Byte.valueOf("F", 16).byteValue());
180
181        try {
182            Byte.valueOf("0x1", 10);
183            fail("Expected NumberFormatException with hex string.");
184        } catch (NumberFormatException e) {
185        }
186
187        try {
188            Byte.valueOf("9.2", 10);
189            fail("Expected NumberFormatException with floating point string.");
190        } catch (NumberFormatException e) {
191        }
192
193        try {
194            Byte.valueOf("", 10);
195            fail("Expected NumberFormatException with empty string.");
196        } catch (NumberFormatException e) {
197        }
198
199        try {
200            Byte.valueOf(null, 10);
201            fail("Expected NumberFormatException with null string.");
202        } catch (NumberFormatException e) {
203        }
204    }
205
206    /**
207     * @tests java.lang.Byte#parseByte(String)
208     */
209    public void test_parseByteLjava_lang_String() {
210        assertEquals(0, Byte.parseByte("0"));
211        assertEquals(1, Byte.parseByte("1"));
212        assertEquals(-1, Byte.parseByte("-1"));
213
214        try {
215            Byte.parseByte("0x1");
216            fail("Expected NumberFormatException with hex string.");
217        } catch (NumberFormatException e) {
218        }
219
220        try {
221            Byte.parseByte("9.2");
222            fail("Expected NumberFormatException with floating point string.");
223        } catch (NumberFormatException e) {
224        }
225
226        try {
227            Byte.parseByte("");
228            fail("Expected NumberFormatException with empty string.");
229        } catch (NumberFormatException e) {
230        }
231
232        try {
233            Byte.parseByte(null);
234            fail("Expected NumberFormatException with null string.");
235        } catch (NumberFormatException e) {
236        }
237    }
238
239    /**
240     * @tests java.lang.Byte#parseByte(String,int)
241     */
242    public void test_parseByteLjava_lang_StringI() {
243        assertEquals(0, Byte.parseByte("0", 10));
244        assertEquals(1, Byte.parseByte("1", 10));
245        assertEquals(-1, Byte.parseByte("-1", 10));
246
247        //must be consistent with Character.digit()
248        assertEquals(Character.digit('1', 2), Byte.parseByte("1", 2));
249        assertEquals(Character.digit('F', 16), Byte.parseByte("F", 16));
250
251        try {
252            Byte.parseByte("0x1", 10);
253            fail("Expected NumberFormatException with hex string.");
254        } catch (NumberFormatException e) {
255        }
256
257        try {
258            Byte.parseByte("9.2", 10);
259            fail("Expected NumberFormatException with floating point string.");
260        } catch (NumberFormatException e) {
261        }
262
263        try {
264            Byte.parseByte("", 10);
265            fail("Expected NumberFormatException with empty string.");
266        } catch (NumberFormatException e) {
267        }
268
269        try {
270            Byte.parseByte(null, 10);
271            fail("Expected NumberFormatException with null string.");
272        } catch (NumberFormatException e) {
273        }
274    }
275
276    /**
277     * @tests java.lang.Byte#decode(String)
278     */
279    public void test_decodeLjava_lang_String() {
280        assertEquals(new Byte((byte) 0), Byte.decode("0"));
281        assertEquals(new Byte((byte) 1), Byte.decode("1"));
282        assertEquals(new Byte((byte) -1), Byte.decode("-1"));
283        assertEquals(new Byte((byte) 0xF), Byte.decode("0xF"));
284        assertEquals(new Byte((byte) 0xF), Byte.decode("#F"));
285        assertEquals(new Byte((byte) 0xF), Byte.decode("0XF"));
286        assertEquals(new Byte((byte) 07), Byte.decode("07"));
287
288        try {
289            Byte.decode("9.2");
290            fail("Expected NumberFormatException with floating point string.");
291        } catch (NumberFormatException e) {
292        }
293
294        try {
295            Byte.decode("");
296            fail("Expected NumberFormatException with empty string.");
297        } catch (NumberFormatException e) {
298        }
299
300        try {
301            Byte.decode(null);
302            //undocumented NPE, but seems consistent across JREs
303            fail("Expected NullPointerException with null string.");
304        } catch (NullPointerException e) {
305        }
306    }
307
308    /**
309     * @tests java.lang.Byte#doubleValue()
310     */
311    public void test_doubleValue() {
312        assertEquals(-1D, new Byte((byte) -1).doubleValue(), 0D);
313        assertEquals(0D, new Byte((byte) 0).doubleValue(), 0D);
314        assertEquals(1D, new Byte((byte) 1).doubleValue(), 0D);
315    }
316
317    /**
318     * @tests java.lang.Byte#floatValue()
319     */
320    public void test_floatValue() {
321        assertEquals(-1F, new Byte((byte) -1).floatValue(), 0F);
322        assertEquals(0F, new Byte((byte) 0).floatValue(), 0F);
323        assertEquals(1F, new Byte((byte) 1).floatValue(), 0F);
324    }
325
326    /**
327     * @tests java.lang.Byte#intValue()
328     */
329    public void test_intValue() {
330        assertEquals(-1, new Byte((byte) -1).intValue());
331        assertEquals(0, new Byte((byte) 0).intValue());
332        assertEquals(1, new Byte((byte) 1).intValue());
333    }
334
335    /**
336     * @tests java.lang.Byte#longValue()
337     */
338    public void test_longValue() {
339        assertEquals(-1L, new Byte((byte) -1).longValue());
340        assertEquals(0L, new Byte((byte) 0).longValue());
341        assertEquals(1L, new Byte((byte) 1).longValue());
342    }
343
344    /**
345     * @tests java.lang.Byte#shortValue()
346     */
347    public void test_shortValue() {
348        assertEquals(-1, new Byte((byte) -1).shortValue());
349        assertEquals(0, new Byte((byte) 0).shortValue());
350        assertEquals(1, new Byte((byte) 1).shortValue());
351    }
352
353    /**
354     * @tests java.lang.Byte#compareTo(Byte)
355     */
356    public void test_compareToLjava_lang_Byte() {
357        final Byte min = new Byte(Byte.MIN_VALUE);
358        final Byte zero = new Byte((byte) 0);
359        final Byte max = new Byte(Byte.MAX_VALUE);
360
361        assertTrue(max.compareTo(max) == 0);
362        assertTrue(min.compareTo(min) == 0);
363        assertTrue(zero.compareTo(zero) == 0);
364
365        assertTrue(max.compareTo(zero) > 0);
366        assertTrue(max.compareTo(min) > 0);
367
368        assertTrue(zero.compareTo(max) < 0);
369        assertTrue(zero.compareTo(min) > 0);
370
371        assertTrue(min.compareTo(zero) < 0);
372        assertTrue(min.compareTo(max) < 0);
373
374        try {
375            min.compareTo(null);
376            fail("No NPE");
377        } catch (NullPointerException e) {
378        }
379    }
380
381    /**
382     * @tests java.lang.Byte#Byte(byte)
383     */
384    public void test_ConstructorB2() {
385        // Test for method java.lang.Byte(byte)
386
387        Byte b = new Byte((byte) 127);
388        assertTrue("Byte creation failed", b.byteValue() == (byte) 127);
389    }
390
391    /**
392     * @tests java.lang.Byte#Byte(java.lang.String)
393     */
394    public void test_ConstructorLjava_lang_String2() {
395        // Test for method java.lang.Byte(java.lang.String)
396
397        Byte b = new Byte("127");
398        Byte nb = new Byte("-128");
399        assertTrue("Incorrect Byte Object created", b.byteValue() == (byte) 127
400                && (nb.byteValue() == (byte) -128));
401
402    }
403
404    /**
405     * @tests java.lang.Byte#byteValue()
406     */
407    public void test_byteValue() {
408        // Test for method byte java.lang.Byte.byteValue()
409        assertTrue("Returned incorrect byte value",
410                new Byte((byte) 127).byteValue() == (byte) (127));
411    }
412
413    /**
414     * @tests java.lang.Byte#compareTo(java.lang.Byte)
415     */
416    public void test_compareToLjava_lang_Byte2() {
417        // Test for method int java.lang.Byte.compareTo(java.lang.Byte)
418        assertTrue("Comparison failed", new Byte((byte) 1).compareTo(new Byte((byte) 2)) < 0);
419        assertTrue("Comparison failed", new Byte((byte) 1).compareTo(new Byte((byte) -2)) > 0);
420        assertEquals("Comparison failed", 0, new Byte((byte) 1).compareTo(new Byte((byte) 1)));
421    }
422
423    /**
424     * @tests java.lang.Byte#decode(java.lang.String)
425     */
426    public void test_decodeLjava_lang_String2() {
427        // Test for method java.lang.Byte
428        // java.lang.Byte.decode(java.lang.String)
429        assertTrue("String decoded incorrectly, wanted: 1 got: " + Byte.decode("1").toString(),
430                Byte.decode("1").equals(new Byte((byte) 1)));
431        assertTrue("String decoded incorrectly, wanted: -1 got: "
432                + Byte.decode("-1").toString(), Byte.decode("-1").equals(new Byte((byte) -1)));
433        assertTrue("String decoded incorrectly, wanted: 127 got: "
434                + Byte.decode("127").toString(), Byte.decode("127")
435                .equals(new Byte((byte) 127)));
436        assertTrue("String decoded incorrectly, wanted: -128 got: "
437                + Byte.decode("-128").toString(), Byte.decode("-128").equals(
438                new Byte((byte) -128)));
439        assertTrue("String decoded incorrectly, wanted: 127 got: "
440                + Byte.decode("0x7f").toString(), Byte.decode("0x7f").equals(
441                new Byte((byte) 127)));
442        assertTrue("String decoded incorrectly, wanted: -128 got: "
443                + Byte.decode("-0x80").toString(), Byte.decode("-0x80").equals(
444                new Byte((byte) -128)));
445
446        boolean exception = false;
447        try {
448            Byte.decode("128");
449        } catch (NumberFormatException e) {
450            // Correct
451            exception = true;
452        }
453        assertTrue("Failed to throw exception for MAX_VALUE + 1", exception);
454
455        exception = false;
456        try {
457            Byte.decode("-129");
458        } catch (NumberFormatException e) {
459            // Correct
460            exception = true;
461        }
462        assertTrue("Failed to throw exception for MIN_VALUE - 1", exception);
463
464        exception = false;
465        try {
466            Byte.decode("0x80");
467        } catch (NumberFormatException e) {
468            // Correct
469            exception = true;
470        }
471        assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception);
472
473        exception = false;
474        try {
475            Byte.decode("-0x81");
476        } catch (NumberFormatException e) {
477            // Correct
478            exception = true;
479        }
480        assertTrue("Failed to throw exception for hex MIN_VALUE - 1", exception);
481    }
482
483    /**
484     * @tests java.lang.Byte#doubleValue()
485     */
486    public void test_doubleValue2() {
487        assertEquals(127D, new Byte((byte) 127).doubleValue(), 0.0);
488    }
489
490    /**
491     * @tests java.lang.Byte#equals(java.lang.Object)
492     */
493    public void test_equalsLjava_lang_Object2() {
494        // Test for method boolean java.lang.Byte.equals(java.lang.Object)
495        Byte b1 = new Byte((byte) 90);
496        Byte b2 = new Byte((byte) 90);
497        Byte b3 = new Byte((byte) -90);
498        assertTrue("Equality test failed", b1.equals(b2));
499        assertTrue("Equality test failed", !b1.equals(b3));
500    }
501
502    /**
503     * @tests java.lang.Byte#floatValue()
504     */
505    public void test_floatValue2() {
506        assertEquals(127F, new Byte((byte) 127).floatValue(), 0.0);
507    }
508
509    /**
510     * @tests java.lang.Byte#hashCode()
511     */
512    public void test_hashCode2() {
513        // Test for method int java.lang.Byte.hashCode()
514        assertEquals("Incorrect hash returned", 127, new Byte((byte) 127).hashCode());
515    }
516
517    /**
518     * @tests java.lang.Byte#intValue()
519     */
520    public void test_intValue2() {
521        // Test for method int java.lang.Byte.intValue()
522        assertEquals("Returned incorrect int value", 127, new Byte((byte) 127).intValue());
523    }
524
525    /**
526     * @tests java.lang.Byte#longValue()
527     */
528    public void test_longValue2() {
529        // Test for method long java.lang.Byte.longValue()
530        assertEquals("Returned incorrect long value", 127L, new Byte((byte) 127).longValue());
531    }
532
533    /**
534     * @tests java.lang.Byte#parseByte(java.lang.String)
535     */
536    public void test_parseByteLjava_lang_String2() {
537        assertEquals((byte)127, Byte.parseByte("127"));
538        assertEquals((byte)-128, Byte.parseByte("-128"));
539        assertEquals((byte)0, Byte.parseByte("0"));
540        assertEquals((byte)0x80, Byte.parseByte("-128"));
541        assertEquals((byte)0x7F, Byte.parseByte("127"));
542
543        try {
544            Byte.parseByte("-1000");
545            fail("No NumberFormatException");
546        } catch (NumberFormatException e) {
547        }
548
549        try {
550            Byte.parseByte("128");
551            fail("No NumberFormatException");
552        } catch (NumberFormatException e) {
553        }
554
555        try {
556            Byte.parseByte("-129");
557            fail("No NumberFormatException");
558        } catch (NumberFormatException e) {
559        }
560    }
561
562    /**
563     * @tests java.lang.Byte#parseByte(java.lang.String, int)
564     */
565    public void test_parseByteLjava_lang_StringI2() {
566        // Test for method byte java.lang.Byte.parseByte(java.lang.String, int)
567        byte b = Byte.parseByte("127", 10);
568        byte bn = Byte.parseByte("-128", 10);
569        assertTrue("Invalid parse of dec byte", b == (byte) 127 && (bn == (byte) -128));
570        assertEquals("Failed to parse hex value", 10, Byte.parseByte("A", 16));
571        assertEquals("Returned incorrect value for 0 hex", 0, Byte.parseByte("0", 16));
572        assertTrue("Returned incorrect value for most negative value hex", Byte.parseByte(
573                "-80", 16) == (byte) 0x80);
574        assertTrue("Returned incorrect value for most positive value hex", Byte.parseByte("7f",
575                16) == 0x7f);
576        assertEquals("Returned incorrect value for 0 decimal", 0, Byte.parseByte("0", 10));
577        assertTrue("Returned incorrect value for most negative value decimal", Byte.parseByte(
578                "-128", 10) == (byte) 0x80);
579        assertTrue("Returned incorrect value for most positive value decimal", Byte.parseByte(
580                "127", 10) == 0x7f);
581
582        try {
583            Byte.parseByte("-1000", 10);
584            fail("Failed to throw exception");
585        } catch (NumberFormatException e) {
586        }
587
588        try {
589            Byte.parseByte("128", 10);
590            fail("Failed to throw exception for MAX_VALUE + 1");
591        } catch (NumberFormatException e) {
592        }
593
594        try {
595            Byte.parseByte("-129", 10);
596            fail("Failed to throw exception for MIN_VALUE - 1");
597        } catch (NumberFormatException e) {
598        }
599
600        try {
601            Byte.parseByte("80", 16);
602            fail("Failed to throw exception for hex MAX_VALUE + 1");
603        } catch (NumberFormatException e) {
604        }
605
606        try {
607            Byte.parseByte("-81", 16);
608            fail("Failed to throw exception for hex MIN_VALUE + 1");
609        } catch (NumberFormatException e) {
610        }
611    }
612
613    /**
614     * @tests java.lang.Byte#shortValue()
615     */
616    public void test_shortValue2() {
617        assertEquals((short)127, new Byte((byte)127).shortValue());
618    }
619
620    /**
621     * @tests java.lang.Byte#toString()
622     */
623    public void test_toString2() {
624        assertEquals("Returned incorrect String", "127", new Byte((byte) 127).toString());
625        assertEquals("Returned incorrect String", "-127", new Byte((byte) -127).toString());
626        assertEquals("Returned incorrect String", "-128", new Byte((byte) -128).toString());
627    }
628
629    /**
630     * @tests java.lang.Byte#toString(byte)
631     */
632    public void test_toStringB2() {
633        assertEquals("Returned incorrect String", "127", Byte.toString((byte) 127));
634        assertEquals("Returned incorrect String", "-127", Byte.toString((byte) -127));
635        assertEquals("Returned incorrect String", "-128", Byte.toString((byte) -128));
636    }
637
638    /**
639     * @tests java.lang.Byte#valueOf(java.lang.String)
640     */
641    public void test_valueOfLjava_lang_String2() {
642        assertEquals("Returned incorrect byte", 0, Byte.valueOf("0").byteValue());
643        assertEquals("Returned incorrect byte", 127, Byte.valueOf("127").byteValue());
644        assertEquals("Returned incorrect byte", -127, Byte.valueOf("-127").byteValue());
645        assertEquals("Returned incorrect byte", -128, Byte.valueOf("-128").byteValue());
646
647        try {
648            Byte.valueOf("128");
649            fail("Failed to throw exception when passes value > byte");
650        } catch (NumberFormatException e) {
651        }
652    }
653
654    /**
655     * @tests java.lang.Byte#valueOf(java.lang.String, int)
656     */
657    public void test_valueOfLjava_lang_StringI2() {
658        assertEquals("Returned incorrect byte", 10, Byte.valueOf("A", 16).byteValue());
659        assertEquals("Returned incorrect byte", 127, Byte.valueOf("127", 10).byteValue());
660        assertEquals("Returned incorrect byte", -127, Byte.valueOf("-127", 10).byteValue());
661        assertEquals("Returned incorrect byte", -128, Byte.valueOf("-128", 10).byteValue());
662        assertEquals("Returned incorrect byte", 127, Byte.valueOf("7f", 16).byteValue());
663        assertEquals("Returned incorrect byte", -127, Byte.valueOf("-7f", 16).byteValue());
664        assertEquals("Returned incorrect byte", -128, Byte.valueOf("-80", 16).byteValue());
665
666        try {
667            Byte.valueOf("128", 10);
668            fail("Failed to throw exception when passes value > byte");
669        } catch (NumberFormatException e) {
670        }
671    }
672}
673