probability_entry_test.cpp revision 88bc312ad34321fb3e81be2dc939a889d065f4a7
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/structure/v4/content/probability_entry.h"
18
19#include <gtest/gtest.h>
20
21#include "defines.h"
22
23namespace latinime {
24namespace {
25
26TEST(ProbabilityEntryTest, TestEncodeDecode) {
27    const int flag = 0xFF;
28    const int probability = 10;
29
30    const ProbabilityEntry entry(flag, probability);
31    const uint64_t encodedEntry = entry.encode(false /* hasHistoricalInfo */);
32    const ProbabilityEntry decodedEntry =
33            ProbabilityEntry::decode(encodedEntry, false /* hasHistoricalInfo */);
34    EXPECT_EQ(0xFF0Aull, encodedEntry);
35    EXPECT_EQ(flag, decodedEntry.getFlags());
36    EXPECT_EQ(probability, decodedEntry.getProbability());
37}
38
39TEST(ProbabilityEntryTest, TestEncodeDecodeWithHistoricalInfo) {
40    const int flag = 0xF0;
41    const int timestamp = 0x3FFFFFFF;
42    const int count = 0xABCD;
43
44    const HistoricalInfo historicalInfo(timestamp, 0 /* level */, count);
45    const ProbabilityEntry entry(flag, &historicalInfo);
46
47    const uint64_t encodedEntry = entry.encode(true /* hasHistoricalInfo */);
48    EXPECT_EQ(0xF03FFFFFFFABCDull, encodedEntry);
49    const ProbabilityEntry decodedEntry =
50            ProbabilityEntry::decode(encodedEntry, true /* hasHistoricalInfo */);
51
52    EXPECT_EQ(flag, decodedEntry.getFlags());
53    EXPECT_EQ(timestamp, decodedEntry.getHistoricalInfo()->getTimestamp());
54    EXPECT_EQ(count, decodedEntry.getHistoricalInfo()->getCount());
55}
56
57}  // namespace
58}  // namespace latinime
59