77 lines · cpp
1//===- llvm/unittest/Frontend/PropertySetRegistry.cpp ---------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/ADT/SmallVector.h"10#include "llvm/Frontend/Offloading/PropertySet.h"11#include "llvm/Support/MemoryBuffer.h"12#include "gtest/gtest.h"13 14using namespace llvm::offloading;15using namespace llvm;16 17void checkSerialization(const PropertySetRegistry &PSR) {18 SmallString<0> Serialized;19 raw_svector_ostream OS(Serialized);20 writePropertiesToJSON(PSR, OS);21 auto PSR2 = readPropertiesFromJSON({Serialized, ""});22 ASSERT_EQ("", toString(PSR2.takeError()));23 EXPECT_EQ(PSR, *PSR2);24}25 26TEST(PropertySetRegistryTest, PropertySetRegistry) {27 PropertySetRegistry PSR;28 checkSerialization(PSR);29 30 PSR["Category1"]["Prop1"] = 42U;31 PSR["Category1"]["Prop2"] = ByteArray(StringRef("Hello").bytes());32 PSR["Category2"]["A"] = ByteArray{0, 4, 16, 32, 255};33 checkSerialization(PSR);34 35 PSR = PropertySetRegistry();36 PSR["ABC"]["empty_array"] = ByteArray();37 PSR["ABC"]["max_val"] = std::numeric_limits<uint32_t>::max();38 checkSerialization(PSR);39}40 41TEST(PropertySetRegistryTest, IllFormedJSON) {42 SmallString<0> Input;43 44 // Invalid json45 Input = "{ invalid }";46 auto Res = readPropertiesFromJSON({Input, ""});47 EXPECT_NE("", toString(Res.takeError()));48 49 Input = "";50 Res = readPropertiesFromJSON({Input, ""});51 EXPECT_NE("", toString(Res.takeError()));52 53 // Not a JSON object54 Input = "[1, 2, 3]";55 Res = readPropertiesFromJSON({Input, ""});56 EXPECT_NE("", toString(Res.takeError()));57 58 // Property set not an object59 Input = R"({ "Category": 42 })";60 Res = readPropertiesFromJSON({Input, ""});61 EXPECT_NE("", toString(Res.takeError()));62 63 // Property value has non string/non-integer type64 Input = R"({ "Category": { "Prop": [1, 2, 3] } })";65 Res = readPropertiesFromJSON({Input, ""});66 EXPECT_NE("", toString(Res.takeError()));67 68 // Property value is an invalid base64 string69 Input = R"({ "Category": { "Prop": ";" } })";70 Res = readPropertiesFromJSON({Input, ""});71 EXPECT_NE("", toString(Res.takeError()));72 73 Input = R"({ "Category": { "Prop": "!@#$" } })";74 Res = readPropertiesFromJSON({Input, ""});75 EXPECT_NE("", toString(Res.takeError()));76}77