/* * Copyright 2017, 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 "module.h" #include "file_utils.h" #include "instructions.h" #include "test_utils.h" #include "gtest/gtest.h" #include #include namespace android { namespace spirit { class ModuleTest : public ::testing::Test { protected: virtual void SetUp() { mWordsGlobal = readWords("global.spv"); mWordsGreyscale = readWords("greyscale.spv"); mWordsGreyscale2 = readWords("greyscale2.spv"); mWordsInvert = readWords("invert.spv"); } std::vector mWordsGlobal; std::vector mWordsGreyscale; std::vector mWordsGreyscale2; std::vector mWordsInvert; private: std::vector readWords(const char *testFile) { static const std::string testDataPath( "frameworks/rs/rsov/compiler/spirit/test_data/"); const std::string &fullPath = getAbsolutePath(testDataPath + testFile); return readFile(fullPath); } }; TEST_F(ModuleTest, testDeserialization1) { auto m = Deserialize(mWordsGreyscale); ASSERT_NE(nullptr, m); std::unique_ptr mDeleter(m); int count = 0; std::unique_ptr v( CreateInstructionVisitor([&count](Instruction *) -> void { count++; })); v->visit(m); ASSERT_EQ(count, 123); // TODO:: checkCountEntity() does not work correctly // EXPECT_TRUE(checkCountEntity(m, 123)); EXPECT_EQ(5, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(5, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(11, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(11, countEntity(m)); EXPECT_EQ(4, countEntity(m)); EXPECT_EQ(4, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(14, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(3, countEntity(m)); EXPECT_EQ(6, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(10, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); EXPECT_EQ(4, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(9, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(1, countEntity(m)); EXPECT_EQ(2, countEntity(m)); } TEST_F(ModuleTest, testDeserialization2) { Module *m = Deserialize(mWordsInvert); ASSERT_NE(nullptr, m); std::unique_ptr mDeleter(m); auto outwords = Serialize(m); EXPECT_TRUE(mWordsInvert == outwords); } TEST_F(ModuleTest, testSerialization1) { Module *m = Deserialize(mWordsGreyscale); ASSERT_NE(nullptr, m); std::unique_ptr mDeleter(m); EXPECT_EQ(2, countEntity(m)); auto outwords = Serialize(m); EXPECT_TRUE(mWordsGreyscale == outwords); } TEST_F(ModuleTest, testSerialization2) { Module *m = Deserialize(mWordsGreyscale2); ASSERT_NE(nullptr, m); std::unique_ptr mDeleter(m); EXPECT_EQ(1, countEntity(m)); auto outwords = Serialize(m); EXPECT_TRUE(mWordsGreyscale2 == outwords); } TEST_F(ModuleTest, testLookupByName) { Module *m = Deserialize(mWordsGreyscale); ASSERT_NE(nullptr, m); std::unique_ptr mDeleter(m); m->resolveIds(); Instruction *mainFunc = m->lookupByName("main"); EXPECT_NE(nullptr, mainFunc); EXPECT_STREQ("main", m->lookupNameByInstruction(mainFunc)); auto i = static_cast(m->lookupByName("greyscale(vf4;")); ASSERT_NE(nullptr, i); auto kernel = m->getFunctionDefinitionFromInstruction(i); ASSERT_NE(nullptr, kernel); EXPECT_NE(nullptr, kernel->getParameter(0)); EXPECT_NE(nullptr, kernel->getReturnType()); EXPECT_NE(nullptr, m->lookupFunctionDefinitionByName("greyscale(vf4;")); } TEST_F(ModuleTest, testGetSize) { std::unique_ptr m(new Module()); EXPECT_EQ(4UL, m->getSize(m->getIntType(32))); EXPECT_EQ(4UL, m->getSize(m->getIntType(32, 0))); EXPECT_EQ(4UL, m->getSize(m->getFloatType(32))); EXPECT_EQ(16UL, m->getSize(m->getVectorType(m->getFloatType(32), 4))); } TEST_F(ModuleTest, testFindStringOfPrefix) { Module *m = Deserialize(mWordsGlobal); ASSERT_NE(nullptr, m); std::unique_ptr mDeleter(m); ASSERT_STREQ(".rsov.ExportedVars:0;", m->findStringOfPrefix(".rsov.ExportedVars:").c_str()); } } // namespace spirit } // namespace android