|
| 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 | + * http://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 | +package org.apache.fontbox.cmap; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +class CMapStringsTest |
| 28 | +{ |
| 29 | + |
| 30 | + @Test |
| 31 | + void getNonCachedMappings() |
| 32 | + { |
| 33 | + // arrays consisting of more than 2 bytes aren't cached. |
| 34 | + assertNull(CMapStrings.getMapping(new byte[] { 0, 0, 0 })); |
| 35 | + assertNull(CMapStrings.getMapping(new byte[] { 0, 0, 0, 0 })); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void getMappingOneByte() |
| 40 | + { |
| 41 | + byte[] minValueOneByte = new byte[] { 0 }; |
| 42 | + String minValueMapping = new String(minValueOneByte, StandardCharsets.ISO_8859_1); |
| 43 | + // the given values are equal |
| 44 | + assertEquals(CMapStrings.getMapping(minValueOneByte), |
| 45 | + CMapStrings.getMapping(minValueOneByte)); |
| 46 | + // the given values are the same objects |
| 47 | + assertTrue( |
| 48 | + CMapStrings.getMapping(minValueOneByte) == CMapStrings.getMapping(minValueOneByte)); |
| 49 | + // check the mapped string value |
| 50 | + assertEquals(minValueMapping, CMapStrings.getMapping(minValueOneByte)); |
| 51 | + |
| 52 | + byte[] maxValueOneByte = new byte[] { (byte) 0xff }; |
| 53 | + String maxValueMapping = new String(maxValueOneByte, StandardCharsets.ISO_8859_1); |
| 54 | + assertEquals(CMapStrings.getMapping(maxValueOneByte), |
| 55 | + CMapStrings.getMapping(maxValueOneByte)); |
| 56 | + assertTrue( |
| 57 | + CMapStrings.getMapping(maxValueOneByte) == CMapStrings.getMapping(maxValueOneByte)); |
| 58 | + assertEquals(maxValueMapping, CMapStrings.getMapping(maxValueOneByte)); |
| 59 | + |
| 60 | + byte[] anyValueOneByte = new byte[] { 98 }; |
| 61 | + String anyValueMapping = new String(anyValueOneByte, StandardCharsets.ISO_8859_1); |
| 62 | + assertEquals(CMapStrings.getMapping(anyValueOneByte), |
| 63 | + CMapStrings.getMapping(anyValueOneByte)); |
| 64 | + assertTrue( |
| 65 | + CMapStrings.getMapping(anyValueOneByte) == CMapStrings.getMapping(anyValueOneByte)); |
| 66 | + assertEquals(anyValueMapping, CMapStrings.getMapping(anyValueOneByte)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void getMappingTwoByte() |
| 71 | + { |
| 72 | + byte[] minValueTwoByte = new byte[] { 0, 0 }; |
| 73 | + String minValueMapping = new String(minValueTwoByte, StandardCharsets.UTF_16BE); |
| 74 | + // the given values are equal |
| 75 | + assertEquals(CMapStrings.getMapping(minValueTwoByte), |
| 76 | + CMapStrings.getMapping(minValueTwoByte)); |
| 77 | + // the given values are the same objects |
| 78 | + assertTrue( |
| 79 | + CMapStrings.getMapping(minValueTwoByte) == CMapStrings.getMapping(minValueTwoByte)); |
| 80 | + // check the mapped string value |
| 81 | + assertEquals(minValueMapping, CMapStrings.getMapping(minValueTwoByte)); |
| 82 | + |
| 83 | + byte[] maxValueTwoByte = new byte[] { (byte) 0xff, (byte) 0xff }; |
| 84 | + String maxValueMapping = new String(maxValueTwoByte, StandardCharsets.UTF_16BE); |
| 85 | + assertEquals(CMapStrings.getMapping(maxValueTwoByte), |
| 86 | + CMapStrings.getMapping(maxValueTwoByte)); |
| 87 | + assertTrue( |
| 88 | + CMapStrings.getMapping(maxValueTwoByte) == CMapStrings.getMapping(maxValueTwoByte)); |
| 89 | + assertEquals(maxValueMapping, CMapStrings.getMapping(maxValueTwoByte)); |
| 90 | + |
| 91 | + byte[] anyValueTwoByte1 = new byte[] { 0x62, 0x43 }; |
| 92 | + String anyValueMapping1 = new String(anyValueTwoByte1, StandardCharsets.UTF_16BE); |
| 93 | + assertEquals(CMapStrings.getMapping(anyValueTwoByte1), |
| 94 | + CMapStrings.getMapping(anyValueTwoByte1)); |
| 95 | + assertTrue(CMapStrings.getMapping(anyValueTwoByte1) == CMapStrings |
| 96 | + .getMapping(anyValueTwoByte1)); |
| 97 | + assertEquals(anyValueMapping1, CMapStrings.getMapping(anyValueTwoByte1)); |
| 98 | + |
| 99 | + byte[] anyValueTwoByte2 = new byte[] { (byte) 0xff, 0x43 }; |
| 100 | + String anyValueMapping2 = new String(anyValueTwoByte2, StandardCharsets.UTF_16BE); |
| 101 | + assertEquals(CMapStrings.getMapping(anyValueTwoByte2), |
| 102 | + CMapStrings.getMapping(anyValueTwoByte2)); |
| 103 | + assertTrue(CMapStrings.getMapping(anyValueTwoByte2) == CMapStrings |
| 104 | + .getMapping(anyValueTwoByte2)); |
| 105 | + assertEquals(anyValueMapping2, CMapStrings.getMapping(anyValueTwoByte2)); |
| 106 | + |
| 107 | + byte[] anyValueTwoByte3 = new byte[] { 0x38, (byte) 0xff }; |
| 108 | + String anyValueMapping3 = new String(anyValueTwoByte3, StandardCharsets.UTF_16BE); |
| 109 | + assertEquals(CMapStrings.getMapping(anyValueTwoByte3), |
| 110 | + CMapStrings.getMapping(anyValueTwoByte3)); |
| 111 | + assertTrue(CMapStrings.getMapping(anyValueTwoByte3) == CMapStrings |
| 112 | + .getMapping(anyValueTwoByte3)); |
| 113 | + assertEquals(anyValueMapping3, CMapStrings.getMapping(anyValueTwoByte3)); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void getByteValuesOneByte() |
| 118 | + { |
| 119 | + byte[] minValueOneByte = new byte[] { 0 }; |
| 120 | + // the given values are equal |
| 121 | + assertEquals(CMapStrings.getByteValue(minValueOneByte), |
| 122 | + CMapStrings.getByteValue(minValueOneByte)); |
| 123 | + // the given values are the same objects |
| 124 | + assertTrue(CMapStrings.getByteValue(minValueOneByte) == CMapStrings |
| 125 | + .getByteValue(minValueOneByte)); |
| 126 | + // the cached value isn't the same object than the given one |
| 127 | + assertTrue(minValueOneByte != CMapStrings.getByteValue(minValueOneByte)); |
| 128 | + |
| 129 | + |
| 130 | + byte[] maxValueOneByte = new byte[] { (byte) 0xff }; |
| 131 | + assertEquals(CMapStrings.getByteValue(maxValueOneByte), |
| 132 | + CMapStrings.getByteValue(maxValueOneByte)); |
| 133 | + assertTrue(CMapStrings.getByteValue(maxValueOneByte) == CMapStrings |
| 134 | + .getByteValue(maxValueOneByte)); |
| 135 | + assertTrue(maxValueOneByte != CMapStrings.getByteValue(maxValueOneByte)); |
| 136 | + |
| 137 | + byte[] anyValueOneByte = new byte[] { 98 }; |
| 138 | + assertEquals(CMapStrings.getByteValue(anyValueOneByte), |
| 139 | + CMapStrings.getByteValue(anyValueOneByte)); |
| 140 | + assertTrue(CMapStrings.getByteValue(anyValueOneByte) == CMapStrings |
| 141 | + .getByteValue(anyValueOneByte)); |
| 142 | + assertTrue(anyValueOneByte != CMapStrings.getByteValue(anyValueOneByte)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void getByteValuesTwoByte() |
| 147 | + { |
| 148 | + byte[] minValueTwoByte = new byte[] { 0, 0 }; |
| 149 | + // the given values are equal |
| 150 | + assertEquals(CMapStrings.getByteValue(minValueTwoByte), |
| 151 | + CMapStrings.getByteValue(minValueTwoByte)); |
| 152 | + // the given values are the same objects |
| 153 | + assertTrue(CMapStrings.getByteValue(minValueTwoByte) == CMapStrings |
| 154 | + .getByteValue(minValueTwoByte)); |
| 155 | + // the cached value isn't the same object than the given one |
| 156 | + assertTrue(minValueTwoByte != CMapStrings.getByteValue(minValueTwoByte)); |
| 157 | + |
| 158 | + byte[] maxValueTwoByte = new byte[] { (byte) 0xff, (byte) 0xff }; |
| 159 | + assertEquals(CMapStrings.getByteValue(maxValueTwoByte), |
| 160 | + CMapStrings.getByteValue(maxValueTwoByte)); |
| 161 | + assertTrue(CMapStrings.getByteValue(maxValueTwoByte) == CMapStrings |
| 162 | + .getByteValue(maxValueTwoByte)); |
| 163 | + assertTrue(maxValueTwoByte != CMapStrings.getByteValue(maxValueTwoByte)); |
| 164 | + |
| 165 | + byte[] anyValueTwoByte1 = new byte[] { 0x62, 0x43 }; |
| 166 | + assertEquals(CMapStrings.getByteValue(anyValueTwoByte1), |
| 167 | + CMapStrings.getByteValue(anyValueTwoByte1)); |
| 168 | + assertTrue(CMapStrings.getByteValue(anyValueTwoByte1) == CMapStrings |
| 169 | + .getByteValue(anyValueTwoByte1)); |
| 170 | + assertTrue(anyValueTwoByte1 != CMapStrings.getByteValue(anyValueTwoByte1)); |
| 171 | + |
| 172 | + byte[] anyValueTwoByte2 = new byte[] { (byte) 0xff, 0x43 }; |
| 173 | + assertEquals(CMapStrings.getByteValue(anyValueTwoByte2), |
| 174 | + CMapStrings.getByteValue(anyValueTwoByte2)); |
| 175 | + assertTrue(CMapStrings.getByteValue(anyValueTwoByte2) == CMapStrings |
| 176 | + .getByteValue(anyValueTwoByte2)); |
| 177 | + assertTrue(anyValueTwoByte2 != CMapStrings.getByteValue(anyValueTwoByte2)); |
| 178 | + |
| 179 | + byte[] anyValueTwoByte3 = new byte[] { 0x38, (byte) 0xff }; |
| 180 | + assertEquals(CMapStrings.getByteValue(anyValueTwoByte3), |
| 181 | + CMapStrings.getByteValue(anyValueTwoByte3)); |
| 182 | + assertTrue(CMapStrings.getByteValue(anyValueTwoByte3) == CMapStrings |
| 183 | + .getByteValue(anyValueTwoByte3)); |
| 184 | + assertTrue(anyValueTwoByte3 != CMapStrings.getByteValue(anyValueTwoByte3)); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void getNonCachedByteValues() |
| 189 | + { |
| 190 | + // arrays consisting of more than 2 bytes aren't cached. |
| 191 | + assertNull(CMapStrings.getByteValue(new byte[] { 0, 0, 0 })); |
| 192 | + assertNull(CMapStrings.getByteValue(new byte[] { 0, 0, 0, 0 })); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + void getIndexValuesOneByte() |
| 197 | + { |
| 198 | + byte[] minValueOneByte = new byte[] { 0 }; |
| 199 | + // the given values are equal |
| 200 | + assertEquals(CMapStrings.getIndexValue(minValueOneByte), |
| 201 | + CMapStrings.getIndexValue(minValueOneByte)); |
| 202 | + // the given values are the same objects |
| 203 | + assertTrue(CMapStrings.getIndexValue(minValueOneByte) == CMapStrings |
| 204 | + .getIndexValue(minValueOneByte)); |
| 205 | + // check the int value |
| 206 | + assertEquals(0, CMapStrings.getIndexValue(minValueOneByte)); |
| 207 | + |
| 208 | + byte[] maxValueOneByte = new byte[] { (byte) 0xff }; |
| 209 | + assertEquals(CMapStrings.getIndexValue(maxValueOneByte), |
| 210 | + CMapStrings.getIndexValue(maxValueOneByte)); |
| 211 | + assertTrue(CMapStrings.getIndexValue(maxValueOneByte) == CMapStrings |
| 212 | + .getIndexValue(maxValueOneByte)); |
| 213 | + assertEquals(0xff, CMapStrings.getIndexValue(maxValueOneByte)); |
| 214 | + |
| 215 | + byte[] anyValueOneByte = new byte[] { 98 }; |
| 216 | + assertEquals(CMapStrings.getIndexValue(anyValueOneByte), |
| 217 | + CMapStrings.getIndexValue(anyValueOneByte)); |
| 218 | + assertTrue(CMapStrings.getIndexValue(anyValueOneByte) == CMapStrings |
| 219 | + .getIndexValue(anyValueOneByte)); |
| 220 | + assertEquals(98, CMapStrings.getIndexValue(anyValueOneByte)); |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + void getIndexValuesTwoByte() |
| 225 | + { |
| 226 | + byte[] minValueTwoByte = new byte[] { 0, 0 }; |
| 227 | + // the given values are equal |
| 228 | + assertEquals(CMapStrings.getIndexValue(minValueTwoByte), |
| 229 | + CMapStrings.getIndexValue(minValueTwoByte)); |
| 230 | + // the given values are the same objects |
| 231 | + assertTrue(CMapStrings.getIndexValue(minValueTwoByte) == CMapStrings |
| 232 | + .getIndexValue(minValueTwoByte)); |
| 233 | + // check the int value |
| 234 | + assertEquals(0, CMapStrings.getIndexValue(minValueTwoByte)); |
| 235 | + |
| 236 | + byte[] maxValueTwoByte = new byte[] { (byte) 0xff, (byte) 0xff }; |
| 237 | + assertEquals(CMapStrings.getIndexValue(maxValueTwoByte), |
| 238 | + CMapStrings.getIndexValue(maxValueTwoByte)); |
| 239 | + assertTrue(CMapStrings.getIndexValue(maxValueTwoByte) == CMapStrings |
| 240 | + .getIndexValue(maxValueTwoByte)); |
| 241 | + assertEquals(0xffff, CMapStrings.getIndexValue(maxValueTwoByte)); |
| 242 | + |
| 243 | + byte[] anyValueTwoByte1 = new byte[] { 0x62, 0x43 }; |
| 244 | + assertEquals(CMapStrings.getIndexValue(anyValueTwoByte1), |
| 245 | + CMapStrings.getIndexValue(anyValueTwoByte1)); |
| 246 | + assertTrue(CMapStrings.getIndexValue(anyValueTwoByte1) == CMapStrings |
| 247 | + .getIndexValue(anyValueTwoByte1)); |
| 248 | + assertEquals(0x6243, CMapStrings.getIndexValue(anyValueTwoByte1)); |
| 249 | + |
| 250 | + byte[] anyValueTwoByte2 = new byte[] { (byte) 0xff, 0x43 }; |
| 251 | + assertEquals(CMapStrings.getIndexValue(anyValueTwoByte2), |
| 252 | + CMapStrings.getIndexValue(anyValueTwoByte2)); |
| 253 | + assertTrue(CMapStrings.getIndexValue(anyValueTwoByte2) == CMapStrings |
| 254 | + .getIndexValue(anyValueTwoByte2)); |
| 255 | + assertEquals(0xff43, CMapStrings.getIndexValue(anyValueTwoByte2)); |
| 256 | + |
| 257 | + byte[] anyValueTwoByte3 = new byte[] { 0x38, (byte) 0xff }; |
| 258 | + assertEquals(CMapStrings.getIndexValue(anyValueTwoByte3), |
| 259 | + CMapStrings.getIndexValue(anyValueTwoByte3)); |
| 260 | + assertTrue(CMapStrings.getIndexValue(anyValueTwoByte3) == CMapStrings |
| 261 | + .getIndexValue(anyValueTwoByte3)); |
| 262 | + assertEquals(0x38ff, CMapStrings.getIndexValue(anyValueTwoByte3)); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + void getNonCachedIndexValues() |
| 267 | + { |
| 268 | + // arrays consisting of more than 2 bytes aren't cached. |
| 269 | + assertNull(CMapStrings.getIndexValue(new byte[] { 0, 0, 0 })); |
| 270 | + assertNull(CMapStrings.getIndexValue(new byte[] { 0, 0, 0, 0 })); |
| 271 | + } |
| 272 | + |
| 273 | +} |
0 commit comments