88// ===----------------------------------------------------------------------===//
99
1010#include " llvm/Support/SYCLPropertySetIO.h"
11+ #include " llvm/ADT/MapVector.h"
1112#include " llvm/Support/MemoryBuffer.h"
1213#include " llvm/Support/raw_ostream.h"
1314#include " llvm/Testing/Support/Error.h"
@@ -19,70 +20,121 @@ using namespace llvm::util;
1920
2021namespace {
2122
22- TEST (SYCLPropertySet, IncorrectValuesIO ) {
23+ TEST (SYCLPropertySet, IncorrectValuesReadIO ) {
2324 auto Content = " Staff/Ages]\n " ;
2425 auto MemBuf = MemoryBuffer::getMemBuffer (Content);
2526 // Parse a property set registry
2627 auto PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
27- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
28- << " Invalid line" ;
28+ EXPECT_THAT_ERROR (
29+ std::move (PropSetsPtr.takeError ()),
30+ FailedWithMessage (testing::HasSubstr (" property category missing" )));
2931
3032 Content = " [Staff/Ages\n " ;
3133 MemBuf = MemoryBuffer::getMemBuffer (Content);
3234 // Parse a property set registry
3335 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
34- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
35- << " Invalid line" ;
36+ EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()),
37+ FailedWithMessage ( testing::HasSubstr ( " invalid line" ))) ;
3638
3739 Content = " [Staff/Ages]\n "
3840 " person1=\n " ;
3941 MemBuf = MemoryBuffer::getMemBuffer (Content);
4042 // Parse a property set registry
4143 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
42- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
43- << " Invalid property line" ;
44+ EXPECT_THAT_ERROR (
45+ std::move (PropSetsPtr.takeError ()),
46+ FailedWithMessage (testing::HasSubstr (" invalid property line" )));
4447
4548 Content = " [Staff/Ages]\n "
4649 " person1=|10\n " ;
4750 MemBuf = MemoryBuffer::getMemBuffer (Content);
4851 // Parse a property set registry
4952 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
50- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
51- << " Invalid property value" ;
53+ EXPECT_THAT_ERROR (
54+ std::move (PropSetsPtr.takeError ()),
55+ FailedWithMessage (testing::HasSubstr (" invalid property value" )));
5256
5357 Content = " [Staff/Ages]\n "
5458 " person1=abc|10\n " ;
5559 MemBuf = MemoryBuffer::getMemBuffer (Content);
5660 // Parse a property set registry
5761 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
58- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
59- << " Invalid property type" ;
62+ EXPECT_THAT_ERROR (
63+ std::move (PropSetsPtr.takeError ()),
64+ FailedWithMessage (testing::HasSubstr (" invalid property type" )));
6065
6166 Content = " [Staff/Ages]\n "
6267 " person1=1|IAQ\n " ;
6368 MemBuf = MemoryBuffer::getMemBuffer (Content);
6469 // Parse a property set registry
6570 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
66- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
67- << " Invalid property value" ;
71+ EXPECT_THAT_ERROR (
72+ std::move (PropSetsPtr.takeError ()),
73+ FailedWithMessage (testing::HasSubstr (" invalid property value" )));
6874
6975 Content = " [Staff/Ages]\n "
7076 " person1=2|IAQ\n " ;
7177 MemBuf = MemoryBuffer::getMemBuffer (Content);
7278 // Parse a property set registry
7379 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
74- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
75- << " Invalid property value" ;
80+ EXPECT_THAT_ERROR (
81+ std::move (PropSetsPtr.takeError ()),
82+ FailedWithMessage (testing::HasSubstr (
83+ " Base64 encoded strings must be a multiple of 4 bytes in length" )));
7684
7785 Content = " [Staff/Ages]\n "
7886 " person1=100|10\n " ;
7987 MemBuf = MemoryBuffer::getMemBuffer (Content);
8088 // Parse a property set registry
8189 PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
82- EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()), Failed ())
83- << " Invalid property type" ;
90+ EXPECT_THAT_ERROR (std::move (PropSetsPtr.takeError ()),
91+ FailedWithMessage (testing::HasSubstr (" bad property type" )));
92+
93+ Content = " [Opt/Param]\n "
94+ " kernel1=2|IAAAAAAAAAQA\t IAAAAAAAAAQ\n " ;
95+ MemBuf = MemoryBuffer::getMemBuffer (Content);
96+ // Parse a property set registry
97+ PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
98+ EXPECT_THAT_ERROR (
99+ std::move (PropSetsPtr.takeError ()),
100+ FailedWithMessage (testing::HasSubstr (" Invalid Base64 character" )));
101+
102+ Content = " [Opt/Param]\n "
103+ " kernel1=2|IAAAAAAAAAQA\n IAAAAAAAAAQ\n " ;
104+ MemBuf = MemoryBuffer::getMemBuffer (Content);
105+ // Parse a property set registry
106+ PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
107+ EXPECT_THAT_ERROR (
108+ std::move (PropSetsPtr.takeError ()),
109+ FailedWithMessage (testing::HasSubstr (" invalid property line" )));
84110}
85111
112+ TEST (SYCLPropertySet, DuplicateKeysRead) {
113+ // '1' in '1|20' means 'integer property'
114+ auto Content = " [Staff/Ages]\n "
115+ " person1=1|20\n "
116+ " person1=1|25\n " ;
117+ // For duplicate keys, the latest key is selected
118+ auto ExpectedContent = " [Staff/Ages]\n "
119+ " person1=1|25\n " ;
120+ auto MemBuf = MemoryBuffer::getMemBuffer (Content);
121+ // Parse a property set registry
122+ auto PropSetsPtr = SYCLPropertySetRegistry::read (MemBuf.get ());
123+
124+ if (!PropSetsPtr)
125+ FAIL () << " SYCLPropertySetRegistry::read failed\n " ;
126+
127+ std::string Serialized;
128+ {
129+ llvm::raw_string_ostream OS (Serialized);
130+ // Serialize
131+ PropSetsPtr->get ()->write (OS);
132+ }
133+ // Check that the original and the serialized version are equal
134+ EXPECT_EQ (Serialized, ExpectedContent);
135+ }
136+
137+ // Test read-then-write of a list of integer values
86138TEST (SYCLPropertySet, IntValuesIO) {
87139 // '1' in '1|20' means 'integer property'
88140 auto Content = " [Staff/Ages]\n "
@@ -109,6 +161,7 @@ TEST(SYCLPropertySet, IntValuesIO) {
109161 EXPECT_EQ (Serialized, Content);
110162}
111163
164+ // Test read-then-write of a list of byte arrays
112165TEST (SYCLPropertySet, ByteArrayValuesIO) {
113166 // '2' in '2|...' means 'byte array property', Base64-encoded
114167 // encodes the following byte arrays:
@@ -135,4 +188,127 @@ TEST(SYCLPropertySet, ByteArrayValuesIO) {
135188 // Check that the original and the serialized version are equal
136189 EXPECT_EQ (Serialized, Content);
137190}
191+
192+ // Test write-then-read of boolean values
193+ TEST (SYCLPropertySet, Mask) {
194+ SYCLPropertySetRegistry PropSet;
195+ uint32_t Mask = 0 ;
196+ std::map<StringRef, uint32_t > DevMask = {{" Mask" , Mask}};
197+ PropSet.add (" MaskCategory" , DevMask);
198+ std::string Serialized;
199+ std::string Expected (" [MaskCategory]\n Mask=1|0\n " );
200+ {
201+ llvm::raw_string_ostream OS (Serialized);
202+ // Serialize
203+ PropSet.write (OS);
204+ }
205+ llvm::errs () << Serialized << " \n " ;
206+ EXPECT_EQ (Serialized, Expected);
207+ }
208+
209+ // Test write-then-read of std::map<StringRef, SYCLPropertyValue>
210+ // SYClPropertyValue is a class which contains one member, which is
211+ // std::variant<uint32_t, std::unique_ptr<std::byte, Deleter>> Val;
212+ TEST (SYCLPropertySet, SYCLPropertyValues) {
213+ std::map<StringRef, SYCLPropertyValue> PropValues;
214+ std::vector<uint32_t > Values = {1 , 2 , 3 , 4 };
215+ uint32_t Size = 4 ;
216+ PropValues[" Values" ] = std::move (Values);
217+ PropValues[" Size" ] = Size;
218+ SYCLPropertySetRegistry PropSet;
219+ PropSet.add (" Property Values" , PropValues);
220+ std::string Serialized;
221+ std::string Expected (
222+ " [Property Values]\n Size=1|4\n Values=2|AQAAAAIAAAADAAAABAAAAA==\n " );
223+ {
224+ llvm::raw_string_ostream OS (Serialized);
225+ // Serialize
226+ PropSet.write (OS);
227+ }
228+ llvm::errs () << Serialized << " \n " ;
229+ EXPECT_EQ (Serialized, Expected);
230+ }
231+
232+ // Test write-then-read of MapVector of StringRef to a simple struct datatype.
233+ // Example of simple data structure:
234+ // struct SimpleDS {
235+ // unsigned ID;
236+ // unsigned Offset;
237+ // unsigned Size;
238+ // };
239+ struct SimpleDS {
240+ unsigned ID;
241+ unsigned Offset;
242+ unsigned Size;
243+ };
244+ using MapTy = MapVector<StringRef, std::vector<SimpleDS>>;
245+ TEST (SYCLPropertySet, MapToStruct) {
246+ MapTy Values;
247+ std::vector<SimpleDS> SimpleDSVal (2 );
248+ unsigned Start = 0 ;
249+ for (unsigned I = Start; I < Start + 2 ; ++I) {
250+ SimpleDSVal[I - Start].ID = I;
251+ SimpleDSVal[I - Start].Offset = I * 4 ;
252+ SimpleDSVal[I - Start].Size = 4 ;
253+ }
254+ Values[" Value0" ] = SimpleDSVal;
255+ Start += 8 ;
256+ for (unsigned I = Start; I < Start + 2 ; ++I) {
257+ SimpleDSVal[I - Start].ID = I;
258+ SimpleDSVal[I - Start].Offset = I * 4 ;
259+ SimpleDSVal[I - Start].Size = 4 ;
260+ }
261+ Values[" Value1" ] = SimpleDSVal;
262+ SYCLPropertySetRegistry PropSet;
263+ PropSet.add (" Values" , Values);
264+ std::string Serialized;
265+ std::string Expected (
266+ " [Values]\n Value0=2|AAAAAAAAAAAEAAAAAQAAAAQAAAAEAAAA\n Value1=2|"
267+ " CAAAACAAAAAEAAAACQAAACQAAAAEAAAA\n " );
268+ {
269+ llvm::raw_string_ostream OS (Serialized);
270+ // Serialize
271+ PropSet.write (OS);
272+ }
273+ llvm::errs () << Serialized << " \n " ;
274+ EXPECT_EQ (Serialized, Expected);
275+ }
276+
277+ // Test write-then-read of vector of chars
278+ TEST (SYCLPropertySet, VectorOfChars) {
279+ std::vector<char > Values;
280+ for (unsigned I = 0 ; I < 8 ; ++I)
281+ Values.push_back ((char )(I + ' 0' ));
282+ SYCLPropertySetRegistry PropSet;
283+ PropSet.add (" Values" , " all" , Values);
284+ std::string Serialized;
285+ std::string Expected (" [Values]\n all=2|MDEyMzQ1Njc=\n " );
286+ {
287+ llvm::raw_string_ostream OS (Serialized);
288+ // Serialize
289+ PropSet.write (OS);
290+ }
291+ llvm::errs () << Serialized << " \n " ;
292+ EXPECT_EQ (Serialized, Expected);
293+ }
294+
295+ // Test write-then-read of a list of Name-Value pairs
296+ TEST (SYCLPropertySet, ListOfNameValuePairs) {
297+ SYCLPropertySetRegistry PropSet;
298+ std::vector<std::string> Names = {" Name0" , " Name1" , " Name2" , " Name3" };
299+ for (unsigned I = 0 ; I < 4 ; ++I) {
300+ auto Value = I * 8 ;
301+ PropSet.add (" Values" , Names[I], Value);
302+ }
303+ std::string Serialized;
304+ std::string Expected (
305+ " [Values]\n Name0=1|0\n Name1=1|8\n Name2=1|16\n Name3=1|24\n " );
306+ {
307+ llvm::raw_string_ostream OS (Serialized);
308+ // Serialize
309+ PropSet.write (OS);
310+ }
311+ llvm::errs () << Serialized << " \n " ;
312+ EXPECT_EQ (Serialized, Expected);
313+ }
138314} // namespace
0 commit comments