diff options
Diffstat (limited to 'native/jni/tests')
9 files changed, 419 insertions, 3 deletions
diff --git a/native/jni/tests/suggest/core/dicnode/dic_node_pool_test.cpp b/native/jni/tests/suggest/core/dicnode/dic_node_pool_test.cpp new file mode 100644 index 000000000..854efdfe6 --- /dev/null +++ b/native/jni/tests/suggest/core/dicnode/dic_node_pool_test.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "suggest/core/dicnode/dic_node_pool.h" + +#include <gtest/gtest.h> + +namespace latinime { +namespace { + +TEST(DicNodePoolTest, TestGet) { + static const int CAPACITY = 10; + DicNodePool dicNodePool(CAPACITY); + + for (int i = 0; i < CAPACITY; ++i) { + EXPECT_NE(nullptr, dicNodePool.getInstance()); + } + EXPECT_EQ(nullptr, dicNodePool.getInstance()); +} + +TEST(DicNodePoolTest, TestPlaceBack) { + static const int CAPACITY = 1; + DicNodePool dicNodePool(CAPACITY); + + DicNode *const dicNode = dicNodePool.getInstance(); + EXPECT_NE(nullptr, dicNode); + EXPECT_EQ(nullptr, dicNodePool.getInstance()); + dicNodePool.placeBackInstance(dicNode); + EXPECT_EQ(dicNode, dicNodePool.getInstance()); +} + +TEST(DicNodePoolTest, TestReset) { + static const int CAPACITY_SMALL = 2; + static const int CAPACITY_LARGE = 10; + DicNodePool dicNodePool(CAPACITY_SMALL); + + for (int i = 0; i < CAPACITY_SMALL; ++i) { + EXPECT_NE(nullptr, dicNodePool.getInstance()); + } + EXPECT_EQ(nullptr, dicNodePool.getInstance()); + + dicNodePool.reset(CAPACITY_LARGE); + for (int i = 0; i < CAPACITY_LARGE; ++i) { + EXPECT_NE(nullptr, dicNodePool.getInstance()); + } + EXPECT_EQ(nullptr, dicNodePool.getInstance()); + + dicNodePool.reset(CAPACITY_SMALL); + for (int i = 0; i < CAPACITY_SMALL; ++i) { + EXPECT_NE(nullptr, dicNodePool.getInstance()); + } + EXPECT_EQ(nullptr, dicNodePool.getInstance()); +} + +} // namespace +} // namespace latinime diff --git a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp index 6eef2040b..ca8d56f27 100644 --- a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp +++ b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/language_model_dict_content_test.cpp @@ -18,6 +18,8 @@ #include <gtest/gtest.h> +#include <unordered_set> + #include "utils/int_array_view.h" namespace latinime { @@ -35,6 +37,13 @@ TEST(LanguageModelDictContentTest, TestUnigramProbability) { LanguageModelDictContent.getProbabilityEntry(wordId); EXPECT_EQ(flag, entry.getFlags()); EXPECT_EQ(probability, entry.getProbability()); + + // Remove + EXPECT_TRUE(LanguageModelDictContent.removeProbabilityEntry(wordId)); + EXPECT_FALSE(LanguageModelDictContent.getProbabilityEntry(wordId).isValid()); + EXPECT_FALSE(LanguageModelDictContent.removeProbabilityEntry(wordId)); + EXPECT_TRUE(LanguageModelDictContent.setProbabilityEntry(wordId, &probabilityEntry)); + EXPECT_TRUE(LanguageModelDictContent.getProbabilityEntry(wordId).isValid()); } TEST(LanguageModelDictContentTest, TestUnigramProbabilityWithHistoricalInfo) { @@ -46,13 +55,38 @@ TEST(LanguageModelDictContentTest, TestUnigramProbabilityWithHistoricalInfo) { const int count = 10; const int wordId = 100; const HistoricalInfo historicalInfo(timestamp, level, count); - const ProbabilityEntry probabilityEntry(flag, NOT_A_PROBABILITY, &historicalInfo); + const ProbabilityEntry probabilityEntry(flag, &historicalInfo); LanguageModelDictContent.setProbabilityEntry(wordId, &probabilityEntry); const ProbabilityEntry entry = LanguageModelDictContent.getProbabilityEntry(wordId); EXPECT_EQ(flag, entry.getFlags()); EXPECT_EQ(timestamp, entry.getHistoricalInfo()->getTimeStamp()); EXPECT_EQ(level, entry.getHistoricalInfo()->getLevel()); EXPECT_EQ(count, entry.getHistoricalInfo()->getCount()); + + // Remove + EXPECT_TRUE(LanguageModelDictContent.removeProbabilityEntry(wordId)); + EXPECT_FALSE(LanguageModelDictContent.getProbabilityEntry(wordId).isValid()); + EXPECT_FALSE(LanguageModelDictContent.removeProbabilityEntry(wordId)); + EXPECT_TRUE(LanguageModelDictContent.setProbabilityEntry(wordId, &probabilityEntry)); + EXPECT_TRUE(LanguageModelDictContent.removeProbabilityEntry(wordId)); +} + +TEST(LanguageModelDictContentTest, TestIterateProbabilityEntry) { + LanguageModelDictContent languageModelDictContent(false /* useHistoricalInfo */); + + const ProbabilityEntry originalEntry(0xFC, 100); + + const int wordIds[] = { 1, 2, 3, 4, 5 }; + for (const int wordId : wordIds) { + languageModelDictContent.setProbabilityEntry(wordId, &originalEntry); + } + std::unordered_set<int> wordIdSet(std::begin(wordIds), std::end(wordIds)); + for (const auto entry : languageModelDictContent.getProbabilityEntries(WordIdArrayView())) { + EXPECT_EQ(originalEntry.getFlags(), entry.getProbabilityEntry().getFlags()); + EXPECT_EQ(originalEntry.getProbability(), entry.getProbabilityEntry().getProbability()); + wordIdSet.erase(entry.getWordId()); + } + EXPECT_TRUE(wordIdSet.empty()); } } // namespace diff --git a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp index db94550ef..f0494f355 100644 --- a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp +++ b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/probability_entry_test.cpp @@ -43,7 +43,7 @@ TEST(ProbabilityEntryTest, TestEncodeDecodeWithHistoricalInfo) { const int count = 10; const HistoricalInfo historicalInfo(timestamp, level, count); - const ProbabilityEntry entry(flag, NOT_A_PROBABILITY, &historicalInfo); + const ProbabilityEntry entry(flag, &historicalInfo); const uint64_t encodedEntry = entry.encode(true /* hasHistoricalInfo */); EXPECT_EQ(0xF03FFFFFFF030Aull, encodedEntry); diff --git a/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table_test.cpp new file mode 100644 index 000000000..23b9c55f7 --- /dev/null +++ b/native/jni/tests/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table_test.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h" + +#include <gtest/gtest.h> + +#include <vector> + +#include "defines.h" +#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" + +namespace latinime { +namespace { + +TEST(TerminalPositionLookupTableTest, TestGetFromEmptyTable) { + TerminalPositionLookupTable lookupTable; + + EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(0)); + EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(-1)); + EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition( + Ver4DictConstants::NOT_A_TERMINAL_ID)); +} + +TEST(TerminalPositionLookupTableTest, TestSetAndGet) { + TerminalPositionLookupTable lookupTable; + + EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(10, 100)); + EXPECT_EQ(100, lookupTable.getTerminalPtNodePosition(10)); + EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(9)); + EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(9, 200)); + EXPECT_EQ(200, lookupTable.getTerminalPtNodePosition(9)); + EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(10, 300)); + EXPECT_EQ(300, lookupTable.getTerminalPtNodePosition(10)); + EXPECT_FALSE(lookupTable.setTerminalPtNodePosition(-1, 400)); + EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition(-1)); + EXPECT_FALSE(lookupTable.setTerminalPtNodePosition(Ver4DictConstants::NOT_A_TERMINAL_ID, 500)); + EXPECT_EQ(NOT_A_DICT_POS, lookupTable.getTerminalPtNodePosition( + Ver4DictConstants::NOT_A_TERMINAL_ID)); +} + +TEST(TerminalPositionLookupTableTest, TestGC) { + TerminalPositionLookupTable lookupTable; + + const std::vector<int> terminalIds = { 10, 20, 30 }; + const std::vector<int> terminalPositions = { 100, 200, 300 }; + + for (size_t i = 0; i < terminalIds.size(); ++i) { + EXPECT_TRUE(lookupTable.setTerminalPtNodePosition(terminalIds[i], terminalPositions[i])); + } + + TerminalPositionLookupTable::TerminalIdMap terminalIdMap; + EXPECT_TRUE(lookupTable.runGCTerminalIds(&terminalIdMap)); + + for (size_t i = 0; i < terminalIds.size(); ++i) { + EXPECT_EQ(static_cast<int>(i), terminalIdMap[terminalIds[i]]) + << "Terminal id (" << terminalIds[i] << ") should be changed to " << i; + EXPECT_EQ(terminalPositions[i], lookupTable.getTerminalPtNodePosition(i)); + } +} + +} // namespace +} // namespace latinime diff --git a/native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp new file mode 100644 index 000000000..a1c310d8a --- /dev/null +++ b/native/jni/tests/suggest/policyimpl/dictionary/utils/byte_array_utils_test.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "suggest/policyimpl/dictionary/utils/byte_array_utils.h" + +#include <gtest/gtest.h> + +#include <cstdint> + +namespace latinime { +namespace { + +TEST(ByteArrayUtilsTest, TestReadInt) { + const uint8_t buffer[] = { 0x1u, 0x8Au, 0x0u, 0xAAu }; + + EXPECT_EQ(0x01u, ByteArrayUtils::readUint8(buffer, 0)); + EXPECT_EQ(0x8Au, ByteArrayUtils::readUint8(buffer, 1)); + EXPECT_EQ(0x0u, ByteArrayUtils::readUint8(buffer, 2)); + EXPECT_EQ(0xAAu, ByteArrayUtils::readUint8(buffer, 3)); + + EXPECT_EQ(0x018Au, ByteArrayUtils::readUint16(buffer, 0)); + EXPECT_EQ(0x8A00u, ByteArrayUtils::readUint16(buffer, 1)); + EXPECT_EQ(0xAAu, ByteArrayUtils::readUint16(buffer, 2)); + + EXPECT_EQ(0x18A00AAu, ByteArrayUtils::readUint32(buffer, 0)); + + int pos = 0; + EXPECT_EQ(0x18A00, ByteArrayUtils::readSint24AndAdvancePosition(buffer, &pos)); + pos = 1; + EXPECT_EQ(-0xA00AA, ByteArrayUtils::readSint24AndAdvancePosition(buffer, &pos)); +} + +TEST(ByteArrayUtilsTest, TestWriteAndReadInt) { + uint8_t buffer[4]; + + int pos = 0; + const uint8_t data_1B = 0xC8; + ByteArrayUtils::writeUintAndAdvancePosition(buffer, data_1B, 1, &pos); + EXPECT_EQ(data_1B, ByteArrayUtils::readUint(buffer, 1, 0)); + + pos = 0; + const uint32_t data_4B = 0xABCD1234; + ByteArrayUtils::writeUintAndAdvancePosition(buffer, data_4B, 4, &pos); + EXPECT_EQ(data_4B, ByteArrayUtils::readUint(buffer, 4, 0)); +} + +TEST(ByteArrayUtilsTest, TestReadCodePoint) { + const uint8_t buffer[] = { 0x10, 0xFF, 0x00u, 0x20u, 0x41u, 0x1Fu, 0x60 }; + + EXPECT_EQ(0x10FF00, ByteArrayUtils::readCodePoint(buffer, 0)); + EXPECT_EQ(0x20, ByteArrayUtils::readCodePoint(buffer, 3)); + EXPECT_EQ(0x41, ByteArrayUtils::readCodePoint(buffer, 4)); + EXPECT_EQ(NOT_A_CODE_POINT, ByteArrayUtils::readCodePoint(buffer, 5)); + + int pos = 0; + int codePointArray[3]; + EXPECT_EQ(3, ByteArrayUtils::readStringAndAdvancePosition(buffer, MAX_WORD_LENGTH, + codePointArray, &pos)); + EXPECT_EQ(0x10FF00, codePointArray[0]); + EXPECT_EQ(0x20, codePointArray[1]); + EXPECT_EQ(0x41, codePointArray[2]); + EXPECT_EQ(0x60, ByteArrayUtils::readCodePoint(buffer, pos)); +} + +TEST(ByteArrayUtilsTest, TestWriteAndReadCodePoint) { + uint8_t buffer[10]; + + const int codePointArray[] = { 0x10FF00, 0x20, 0x41 }; + int pos = 0; + ByteArrayUtils::writeCodePointsAndAdvancePosition(buffer, codePointArray, 3, + true /* writesTerminator */, &pos); + EXPECT_EQ(0x10FF00, ByteArrayUtils::readCodePoint(buffer, 0)); + EXPECT_EQ(0x20, ByteArrayUtils::readCodePoint(buffer, 3)); + EXPECT_EQ(0x41, ByteArrayUtils::readCodePoint(buffer, 4)); + EXPECT_EQ(NOT_A_CODE_POINT, ByteArrayUtils::readCodePoint(buffer, 5)); +} + +} // namespace +} // namespace latinime diff --git a/native/jni/tests/suggest/policyimpl/dictionary/utils/sparse_table_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/utils/sparse_table_test.cpp new file mode 100644 index 000000000..0b57156a0 --- /dev/null +++ b/native/jni/tests/suggest/policyimpl/dictionary/utils/sparse_table_test.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "suggest/policyimpl/dictionary/utils/sparse_table.h" + +#include <gtest/gtest.h> + +#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" + +namespace latinime { +namespace { + +TEST(SparseTableTest, TestSetAndGet) { + static const int BLOCK_SIZE = 64; + static const int DATA_SIZE = 4; + BufferWithExtendableBuffer indexTableBuffer( + BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE); + BufferWithExtendableBuffer contentTableBuffer( + BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE); + SparseTable sparseTable(&indexTableBuffer, &contentTableBuffer, BLOCK_SIZE, DATA_SIZE); + + EXPECT_FALSE(sparseTable.contains(10)); + EXPECT_TRUE(sparseTable.set(10, 100u)); + EXPECT_EQ(100u, sparseTable.get(10)); + EXPECT_TRUE(sparseTable.contains(10)); + EXPECT_TRUE(sparseTable.contains(BLOCK_SIZE - 1)); + EXPECT_FALSE(sparseTable.contains(BLOCK_SIZE)); + EXPECT_TRUE(sparseTable.set(11, 101u)); + EXPECT_EQ(100u, sparseTable.get(10)); + EXPECT_EQ(101u, sparseTable.get(11)); +} + +} // namespace +} // namespace latinime diff --git a/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp b/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp index df778b6cf..56b5aa985 100644 --- a/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp +++ b/native/jni/tests/suggest/policyimpl/dictionary/utils/trie_map_test.cpp @@ -40,6 +40,7 @@ TEST(TrieMapTest, TestSetAndGet) { trieMap.putRoot(11, 1000); EXPECT_EQ(1000ull, trieMap.getRoot(11).mValue); const int next = trieMap.getNextLevelBitmapEntryIndex(10); + EXPECT_EQ(1000ull, trieMap.getRoot(10).mValue); trieMap.put(9, 9, next); EXPECT_EQ(9ull, trieMap.get(9, next).mValue); EXPECT_FALSE(trieMap.get(11, next).mIsValid); @@ -47,6 +48,33 @@ TEST(TrieMapTest, TestSetAndGet) { EXPECT_EQ(0xFFFFFFFFFull, trieMap.getRoot(0).mValue); } +TEST(TrieMapTest, TestRemove) { + TrieMap trieMap; + trieMap.putRoot(10, 10); + EXPECT_EQ(10ull, trieMap.getRoot(10).mValue); + EXPECT_TRUE(trieMap.remove(10, trieMap.getRootBitmapEntryIndex())); + EXPECT_FALSE(trieMap.getRoot(10).mIsValid); + for (const auto &element : trieMap.getEntriesInRootLevel()) { + EXPECT_TRUE(false); + } + EXPECT_TRUE(trieMap.putRoot(10, 0x3FFFFF)); + EXPECT_FALSE(trieMap.remove(11, trieMap.getRootBitmapEntryIndex())) + << "Should fail if the key does not exist."; + EXPECT_EQ(0x3FFFFFull, trieMap.getRoot(10).mValue); + trieMap.putRoot(12, 11); + const int nextLevel = trieMap.getNextLevelBitmapEntryIndex(10); + trieMap.put(10, 10, nextLevel); + EXPECT_EQ(0x3FFFFFull, trieMap.getRoot(10).mValue); + EXPECT_EQ(10ull, trieMap.get(10, nextLevel).mValue); + EXPECT_TRUE(trieMap.remove(10, trieMap.getRootBitmapEntryIndex())); + const TrieMap::Result result = trieMap.getRoot(10); + EXPECT_FALSE(result.mIsValid); + EXPECT_EQ(TrieMap::INVALID_INDEX, result.mNextLevelBitmapEntryIndex); + EXPECT_EQ(11ull, trieMap.getRoot(12).mValue); + EXPECT_TRUE(trieMap.putRoot(S_INT_MAX, 0xFFFFFFFFFull)); + EXPECT_TRUE(trieMap.remove(S_INT_MAX, trieMap.getRootBitmapEntryIndex())); +} + TEST(TrieMapTest, TestSetAndGetLarge) { static const int ELEMENT_COUNT = 200000; TrieMap trieMap; diff --git a/native/jni/tests/utils/int_array_view_test.cpp b/native/jni/tests/utils/int_array_view_test.cpp index bd843ab02..161df2f43 100644 --- a/native/jni/tests/utils/int_array_view_test.cpp +++ b/native/jni/tests/utils/int_array_view_test.cpp @@ -53,9 +53,41 @@ TEST(IntArrayViewTest, TestConstructFromArray) { TEST(IntArrayViewTest, TestConstructFromObject) { const int object = 10; const auto intArrayView = IntArrayView::fromObject(&object); - EXPECT_EQ(1, intArrayView.size()); + EXPECT_EQ(1u, intArrayView.size()); EXPECT_EQ(object, intArrayView[0]); } +TEST(IntArrayViewTest, TestLimit) { + const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; + IntArrayView intArrayView(intVector); + + EXPECT_TRUE(intArrayView.limit(0).empty()); + EXPECT_EQ(intArrayView.size(), intArrayView.limit(intArrayView.size()).size()); + EXPECT_EQ(intArrayView.size(), intArrayView.limit(1000).size()); + + IntArrayView subView = intArrayView.limit(4); + EXPECT_EQ(4u, subView.size()); + for (size_t i = 0; i < subView.size(); ++i) { + EXPECT_EQ(intVector[i], subView[i]); + } +} + +TEST(IntArrayViewTest, TestSkip) { + const std::vector<int> intVector = {3, 2, 1, 0, -1, -2}; + IntArrayView intArrayView(intVector); + + EXPECT_TRUE(intArrayView.skip(intVector.size()).empty()); + EXPECT_TRUE(intArrayView.skip(intVector.size() + 1).empty()); + EXPECT_EQ(intArrayView.size(), intArrayView.skip(0).size()); + EXPECT_EQ(intArrayView.size(), intArrayView.limit(1000).size()); + + static const size_t SKIP_COUNT = 2; + IntArrayView subView = intArrayView.skip(SKIP_COUNT); + EXPECT_EQ(intVector.size() - SKIP_COUNT, subView.size()); + for (size_t i = 0; i < subView.size(); ++i) { + EXPECT_EQ(intVector[i + SKIP_COUNT], subView[i]); + } +} + } // namespace } // namespace latinime diff --git a/native/jni/tests/utils/time_keeper_test.cpp b/native/jni/tests/utils/time_keeper_test.cpp new file mode 100644 index 000000000..3f54b91f1 --- /dev/null +++ b/native/jni/tests/utils/time_keeper_test.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "utils/time_keeper.h" + +#include <gtest/gtest.h> + +namespace latinime { +namespace { + +TEST(TimeKeeperTest, TestTestMode) { + TimeKeeper::setCurrentTime(); + const int startTime = TimeKeeper::peekCurrentTime(); + static const int TEST_CURRENT_TIME = 100; + TimeKeeper::startTestModeWithForceCurrentTime(TEST_CURRENT_TIME); + EXPECT_EQ(TEST_CURRENT_TIME, TimeKeeper::peekCurrentTime()); + TimeKeeper::setCurrentTime(); + EXPECT_EQ(TEST_CURRENT_TIME, TimeKeeper::peekCurrentTime()); + TimeKeeper::stopTestMode(); + TimeKeeper::setCurrentTime(); + EXPECT_LE(startTime, TimeKeeper::peekCurrentTime()); +} + +} // namespace +} // namespace latinime |