1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 17 #include "dictionary/utils/byte_array_utils.h" 18 19 #include <gtest/gtest.h> 20 21 #include <cstdint> 22 23 namespace latinime { 24 namespace { 25 26 TEST(ByteArrayUtilsTest, TestReadCodePointTable) { 27 const int codePointTable[] = { 0x6f, 0x6b }; 28 const uint8_t buffer[] = { 0x20u, 0x21u, 0x00u, 0x01u, 0x00u }; 29 int pos = 0; 30 // Expect the first entry of codePointTable 31 EXPECT_EQ(0x6f, ByteArrayUtils::readCodePointAndAdvancePosition(buffer, codePointTable, &pos)); 32 // Expect the second entry of codePointTable 33 EXPECT_EQ(0x6b, ByteArrayUtils::readCodePointAndAdvancePosition(buffer, codePointTable, &pos)); 34 // Expect the original code point from buffer[2] to buffer[4], 0x100 35 // It isn't picked from the codePointTable, since it exceeds the range of the codePointTable. 36 EXPECT_EQ(0x100, ByteArrayUtils::readCodePointAndAdvancePosition(buffer, codePointTable, &pos)); 37 } 38 39 TEST(ByteArrayUtilsTest, TestReadInt) { 40 const uint8_t buffer[] = { 0x1u, 0x8Au, 0x0u, 0xAAu }; 41 42 EXPECT_EQ(0x01u, ByteArrayUtils::readUint8(buffer, 0)); 43 EXPECT_EQ(0x8Au, ByteArrayUtils::readUint8(buffer, 1)); 44 EXPECT_EQ(0x0u, ByteArrayUtils::readUint8(buffer, 2)); 45 EXPECT_EQ(0xAAu, ByteArrayUtils::readUint8(buffer, 3)); 46 47 EXPECT_EQ(0x018Au, ByteArrayUtils::readUint16(buffer, 0)); 48 EXPECT_EQ(0x8A00u, ByteArrayUtils::readUint16(buffer, 1)); 49 EXPECT_EQ(0xAAu, ByteArrayUtils::readUint16(buffer, 2)); 50 51 EXPECT_EQ(0x18A00AAu, ByteArrayUtils::readUint32(buffer, 0)); 52 53 int pos = 0; 54 EXPECT_EQ(0x18A00, ByteArrayUtils::readSint24AndAdvancePosition(buffer, &pos)); 55 pos = 1; 56 EXPECT_EQ(-0xA00AA, ByteArrayUtils::readSint24AndAdvancePosition(buffer, &pos)); 57 } 58 59 TEST(ByteArrayUtilsTest, TestWriteAndReadInt) { 60 uint8_t buffer[4]; 61 62 int pos = 0; 63 const uint8_t data_1B = 0xC8; 64 ByteArrayUtils::writeUintAndAdvancePosition(buffer, data_1B, 1, &pos); 65 EXPECT_EQ(data_1B, ByteArrayUtils::readUint(buffer, 1, 0)); 66 67 pos = 0; 68 const uint32_t data_4B = 0xABCD1234; 69 ByteArrayUtils::writeUintAndAdvancePosition(buffer, data_4B, 4, &pos); 70 EXPECT_EQ(data_4B, ByteArrayUtils::readUint(buffer, 4, 0)); 71 } 72 73 TEST(ByteArrayUtilsTest, TestReadCodePoint) { 74 const uint8_t buffer[] = { 0x10, 0xFF, 0x00u, 0x20u, 0x41u, 0x1Fu, 0x60 }; 75 76 EXPECT_EQ(0x10FF00, ByteArrayUtils::readCodePoint(buffer, 0)); 77 EXPECT_EQ(0x20, ByteArrayUtils::readCodePoint(buffer, 3)); 78 EXPECT_EQ(0x41, ByteArrayUtils::readCodePoint(buffer, 4)); 79 EXPECT_EQ(NOT_A_CODE_POINT, ByteArrayUtils::readCodePoint(buffer, 5)); 80 81 int pos = 0; 82 int codePointArray[3]; 83 EXPECT_EQ(3, ByteArrayUtils::readStringAndAdvancePosition(buffer, MAX_WORD_LENGTH, nullptr, 84 codePointArray, &pos)); 85 EXPECT_EQ(0x10FF00, codePointArray[0]); 86 EXPECT_EQ(0x20, codePointArray[1]); 87 EXPECT_EQ(0x41, codePointArray[2]); 88 EXPECT_EQ(0x60, ByteArrayUtils::readCodePoint(buffer, pos)); 89 } 90 91 TEST(ByteArrayUtilsTest, TestWriteAndReadCodePoint) { 92 uint8_t buffer[10]; 93 94 const int codePointArray[] = { 0x10FF00, 0x20, 0x41 }; 95 int pos = 0; 96 ByteArrayUtils::writeCodePointsAndAdvancePosition(buffer, codePointArray, 3, 97 true /* writesTerminator */, &pos); 98 EXPECT_EQ(0x10FF00, ByteArrayUtils::readCodePoint(buffer, 0)); 99 EXPECT_EQ(0x20, ByteArrayUtils::readCodePoint(buffer, 3)); 100 EXPECT_EQ(0x41, ByteArrayUtils::readCodePoint(buffer, 4)); 101 EXPECT_EQ(NOT_A_CODE_POINT, ByteArrayUtils::readCodePoint(buffer, 5)); 102 } 103 104 } // namespace 105 } // namespace latinime 106