// Tests reading and writing of a single long variant from and to binary files TEST_F(TestVariant, FileIOBinaryIOMultipleTypes) { BinaryFileStream stream; // Values we'll be testing for long value1 = 123; std::string value2 = "test"; auto value3 = nullptr; // Initialize variants Variant variant1(value1); Variant variant2(value2); Variant variant3(value3); // Open stream for writing stream.open(filename, std::ios::out); // Write variants stream << variant1; stream << variant2; stream << variant3; // Close stream stream.close(); // Open stream for reading stream.open(filename, std::ios::in); // Initialize a final variant for storage of data read from the stream Variant variant("a value that must not conflict with the tests"); stream >> variant; EXPECT_EQ(variant, value1); stream >> variant; EXPECT_EQ(variant, value2); stream >> variant; EXPECT_EQ(variant, value3); // Lets make sure this was all the data. We need to read again to set the // EOF bit char tc; EXPECT_FALSE(stream.get(tc)); // Ensure EOF EXPECT_TRUE(stream.eof()); // Close stream stream.close(); }
TEST(TestSortUtils, Sort_SortDescription) { SortItems items; CVariant variant1("M Artist"); SortItem item1; item1[FieldArtist] = variant1; CVariant variant2("B Artist"); SortItem item2; item2[FieldArtist] = variant2; CVariant variant3("R Artist"); SortItem item3; item3[FieldArtist] = variant3; CVariant variant4("R Artist"); SortItem item4; item4[FieldArtist] = variant4; CVariant variant5("I Artist"); SortItem item5; item5[FieldArtist] = variant5; CVariant variant6("A Artist"); SortItem item6; item6[FieldArtist] = variant6; CVariant variant7("G Artist"); SortItem item7; item7[FieldArtist] = variant7; items.push_back(item1); items.push_back(item2); items.push_back(item3); items.push_back(item4); items.push_back(item5); items.push_back(item6); items.push_back(item7); SortDescription desc; desc.sortBy = SortByArtist; SortUtils::Sort(desc, items); EXPECT_STREQ("A Artist", items.at(0)[FieldArtist].asString().c_str()); EXPECT_STREQ("B Artist", items.at(1)[FieldArtist].asString().c_str()); EXPECT_STREQ("G Artist", items.at(2)[FieldArtist].asString().c_str()); EXPECT_STREQ("I Artist", items.at(3)[FieldArtist].asString().c_str()); EXPECT_STREQ("M Artist", items.at(4)[FieldArtist].asString().c_str()); EXPECT_STREQ("R Artist", items.at(5)[FieldArtist].asString().c_str()); EXPECT_STREQ("R Artist", items.at(6)[FieldArtist].asString().c_str()); }
TEST(TestSortUtils, Sort_SortBy) { SortItems items; CVariant variant1("M Artist"); SortItem item1; item1[FieldArtist] = variant1; CVariant variant2("B Artist"); SortItem item2; item2[FieldArtist] = variant2; CVariant variant3("R Artist"); SortItem item3; item3[FieldArtist] = variant3; CVariant variant4("R Artist"); SortItem item4; item4[FieldArtist] = variant4; CVariant variant5("I Artist"); SortItem item5; item5[FieldArtist] = variant5; CVariant variant6("A Artist"); SortItem item6; item6[FieldArtist] = variant6; CVariant variant7("G Artist"); SortItem item7; item7[FieldArtist] = variant7; items.push_back(item1); items.push_back(item2); items.push_back(item3); items.push_back(item4); items.push_back(item5); items.push_back(item6); items.push_back(item7); SortUtils::Sort(SortByArtist, SortOrderAscending, SortAttributeNone, items); EXPECT_STREQ("A Artist", items.at(0)[FieldArtist].asString().c_str()); EXPECT_STREQ("B Artist", items.at(1)[FieldArtist].asString().c_str()); EXPECT_STREQ("G Artist", items.at(2)[FieldArtist].asString().c_str()); EXPECT_STREQ("I Artist", items.at(3)[FieldArtist].asString().c_str()); EXPECT_STREQ("M Artist", items.at(4)[FieldArtist].asString().c_str()); EXPECT_STREQ("R Artist", items.at(5)[FieldArtist].asString().c_str()); EXPECT_STREQ("R Artist", items.at(6)[FieldArtist].asString().c_str()); }
TEST_F(VariantTests, testComparison) { //test comparison between two different types of variant OpenAB::Variant variant1("hello"); OpenAB::Variant variant2(123); ASSERT_FALSE(variant1 == variant2); //test comparison between char types variant1.set('a'); variant2.set('a'); ASSERT_TRUE(variant1 == variant2); variant2.set('b'); ASSERT_FALSE(variant1 == variant2); //test comparison between bool types variant1.set(true); variant2.set(true); ASSERT_TRUE(variant1 == variant2); variant2.set(false); ASSERT_FALSE(variant1 == variant2); //test comparison between pointer types variant1.set((void*)0xBAAD); variant2.set((void*)0xBAAD); ASSERT_TRUE(variant1 == variant2); variant2.set((void*)0xF00D); ASSERT_FALSE(variant1 == variant2); //test comparison between string types variant1.set("hello"); variant2.set(std::string("hello")); ASSERT_TRUE(variant1 == variant2); variant2.set(std::string("goodbye")); ASSERT_FALSE(variant1 == variant2); //test comparison between secure string types variant1.set(OpenAB::SecureString("hello")); variant2 = variant1; ASSERT_TRUE(variant1 == variant2); //each secure string will be different even if containing the same string variant2.set(OpenAB::SecureString("hello")); ASSERT_FALSE(variant1 == variant2); //test comparison between float types variant1.set(123.45f); variant2.set(123.45f); ASSERT_TRUE(variant1 == variant2); variant2.set(543.21f); ASSERT_FALSE(variant1 == variant2); //test comparison between double types variant1.set(123.45); variant2.set(123.45); ASSERT_TRUE(variant1 == variant2); variant2.set(543.21); ASSERT_FALSE(variant1 == variant2); //test comparison between int types variant1.set((u_int8_t)123); variant2.set((u_int8_t)123); ASSERT_TRUE(variant1 == variant2); variant2.set((u_int8_t)321); ASSERT_FALSE(variant1 == variant2); variant1.set((u_int16_t)123); variant2.set((u_int16_t)123); ASSERT_TRUE(variant1 == variant2); variant2.set((u_int16_t)321); ASSERT_FALSE(variant1 == variant2); variant1.set((u_int32_t)123); variant2.set((u_int32_t)123); ASSERT_TRUE(variant1 == variant2); variant2.set((u_int32_t)321); ASSERT_FALSE(variant1 == variant2); variant1.set((int8_t)123); variant2.set((int8_t)123); ASSERT_TRUE(variant1 == variant2); variant2.set((int8_t)321); ASSERT_FALSE(variant1 == variant2); variant1.set((int16_t)123); variant2.set((int16_t)123); ASSERT_TRUE(variant1 == variant2); variant2.set((int16_t)321); ASSERT_FALSE(variant1 == variant2); variant1.set((int32_t)123); variant2.set((int32_t)123); ASSERT_TRUE(variant1 == variant2); variant2.set((int32_t)321); ASSERT_FALSE(variant1 == variant2); }
TEST(FunctionRef, OverloadedFunctor) { struct OverloadedFunctor { // variant 1 int operator()(int x) { return 100 + 1 * x; } // variant 2 (const-overload of v1) int operator()(int x) const { return 100 + 2 * x; } // variant 3 int operator()(int x, int) { return 100 + 3 * x; } // variant 4 (const-overload of v3) int operator()(int x, int) const { return 100 + 4 * x; } // variant 5 (non-const, has no const-overload) int operator()(int x, char const*) { return 100 + 5 * x; } // variant 6 (const only) int operator()(int x, std::vector<int> const&) const { return 100 + 6 * x; } }; OverloadedFunctor of; auto const& cof = of; FunctionRef<int(int)> variant1 = of; EXPECT_EQ(100 + 1 * 15, variant1(15)); FunctionRef<int(int)> const cvariant1 = of; EXPECT_EQ(100 + 1 * 15, cvariant1(15)); FunctionRef<int(int)> variant2 = cof; EXPECT_EQ(100 + 2 * 16, variant2(16)); FunctionRef<int(int)> const cvariant2 = cof; EXPECT_EQ(100 + 2 * 16, cvariant2(16)); FunctionRef<int(int, int)> variant3 = of; EXPECT_EQ(100 + 3 * 17, variant3(17, 0)); FunctionRef<int(int, int)> const cvariant3 = of; EXPECT_EQ(100 + 3 * 17, cvariant3(17, 0)); FunctionRef<int(int, int)> variant4 = cof; EXPECT_EQ(100 + 4 * 18, variant4(18, 0)); FunctionRef<int(int, int)> const cvariant4 = cof; EXPECT_EQ(100 + 4 * 18, cvariant4(18, 0)); FunctionRef<int(int, char const*)> variant5 = of; EXPECT_EQ(100 + 5 * 19, variant5(19, "foo")); FunctionRef<int(int, char const*)> const cvariant5 = of; EXPECT_EQ(100 + 5 * 19, cvariant5(19, "foo")); FunctionRef<int(int, std::vector<int> const&)> variant6 = of; EXPECT_EQ(100 + 6 * 20, variant6(20, {})); EXPECT_EQ(100 + 6 * 20, variant6(20, {1, 2, 3})); FunctionRef<int(int, std::vector<int> const&)> const cvariant6 = of; EXPECT_EQ(100 + 6 * 20, cvariant6(20, {})); EXPECT_EQ(100 + 6 * 20, cvariant6(20, {1, 2, 3})); FunctionRef<int(int, std::vector<int> const&)> variant6const = cof; EXPECT_EQ(100 + 6 * 21, variant6const(21, {})); FunctionRef<int(int, std::vector<int> const&)> const cvariant6const = cof; EXPECT_EQ(100 + 6 * 21, cvariant6const(21, {})); }