View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3.text.translate;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import org.apache.commons.lang3.AbstractLangTest;
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.params.ParameterizedTest;
25  import org.junit.jupiter.params.provider.CsvSource;
26  import org.junitpioneer.jupiter.DefaultLocale;
27  
28  /**
29   * Tests {@link CharSequenceTranslator}.
30   */
31  @Deprecated
32  class CharSequenceTranslatorTest extends AbstractLangTest {
33  
34      @ParameterizedTest
35      // @formatter:off
36      @CsvSource({
37          "0, 0", "1, 1", "2, 2", "3, 3", "4, 4", "5, 5", "6, 6", "7, 7",
38          "8, 8", "9, 9", "10, A", "11, B", "12, C", "13, D", "14, E", "15, F",
39          "0x10, 10", "0xFF, FF", "0x100, 100", "0xFFF, FFF", "0x1000, 1000",
40          "0xFFFF, FFFF", "0x10000, 10000", "0xFFFFF, FFFFF", "0x100000, 100000",
41          "0xFFFFFF, FFFFFF", "0x1000000, 1000000", "0xFFFFFFF, FFFFFFF", "0x10000000, 10000000",
42          "0xABCDEF, ABCDEF", "0x1020304, 1020304", "2147483647, 7FFFFFFF"
43      })
44      // @formatter:on
45      void testHexDigitsAndWidth(final int value, final String expected) {
46          assertEquals(expected, CharSequenceTranslator.hex(value));
47      }
48  
49      @Test
50      @DefaultLocale("tr")
51      void testHexIgnoresDefaultLocale() {
52          assertEquals("ABCDEF", CharSequenceTranslator.hex(0xABCDEF));
53          assertEquals("FFFFFFFF", CharSequenceTranslator.hex(-1));
54      }
55  
56      @ParameterizedTest
57      @CsvSource({ "-1, FFFFFFFF", "-2, FFFFFFFE", "-16, FFFFFFF0", "-255, FFFFFF01", "-2147483647, 80000001", "-2147483648, 80000000" })
58      void testHexNegativeValuesUseTwosComplement(final int value, final String expected) {
59          assertEquals(expected, CharSequenceTranslator.hex(value));
60      }
61  
62      @ParameterizedTest
63      // @formatter:off
64      @CsvSource({
65          "0x20, 20", "0x41, 41", "0x7F, 7F", "0x80, 80", "0xE9, E9", "0x20AC, 20AC",
66          "0xD7FF, D7FF", "0xD800, D800", "0xDFFF, DFFF", "0xE000, E000", "0xFFFF, FFFF",
67          "0x10000, 10000", "0x1F600, 1F600", "0x10FFFF, 10FFFF", "0x110000, 110000"
68      })
69      // @formatter:on
70      void testHexUnicodeBoundaries(final int value, final String expected) {
71          // Conversion accepts any int, including surrogates and values beyond the Unicode range.
72          assertEquals(expected, CharSequenceTranslator.hex(value));
73      }
74  }