1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015-2016 The Khronos Group Inc.
4 // Copyright (c) 2015-2016 Valve Corporation
5 // Copyright (c) 2015-2016 LunarG, Inc.
6 // Copyright (c) 2015-2016 Google, Inc.
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 //     http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 ///////////////////////////////////////////////////////////////////////////////
20 
21 #include "vkjson.h"
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <iostream>
27 
28 #define EXPECT(X) if (!(X)) \
29   ReportFailure(__FILE__, __LINE__, #X);
30 
31 #define ASSERT(X) if (!(X)) { \
32   ReportFailure(__FILE__, __LINE__, #X); \
33   return 2; \
34 }
35 
36 int g_failures;
37 
ReportFailure(const char * file,int line,const char * assertion)38 void ReportFailure(const char* file, int line, const char* assertion) {
39   std::cout << file << ":" << line << ": \"" << assertion << "\" failed."
40             << std::endl;
41   ++g_failures;
42 }
43 
main(int argc,char * argv[])44 int main(int argc, char* argv[]) {
45   std::string errors;
46   bool result = false;
47 
48   VkJsonInstance instance;
49   instance.devices.resize(1);
50   VkJsonDevice& device = instance.devices[0];
51 
52   const char name[] = "Test device";
53   memcpy(device.properties.deviceName, name, sizeof(name));
54   device.properties.limits.maxImageDimension1D = 3;
55   device.properties.limits.maxSamplerLodBias = 3.5f;
56   device.properties.limits.bufferImageGranularity = 0x1ffffffffull;
57   device.properties.limits.maxViewportDimensions[0] = 1;
58   device.properties.limits.maxViewportDimensions[1] = 2;
59   VkFormatProperties format_props = {
60       VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT,
61       VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT,
62       VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT};
63   device.formats.insert(std::make_pair(VK_FORMAT_R8_UNORM, format_props));
64   device.formats.insert(std::make_pair(VK_FORMAT_R8G8_UNORM, format_props));
65 
66   std::string json = VkJsonInstanceToJson(instance);
67   std::cout << json << std::endl;
68 
69   VkJsonInstance instance2;
70   result = VkJsonInstanceFromJson(json, &instance2, &errors);
71   EXPECT(result);
72   if (!result)
73     std::cout << "Error: " << errors << std::endl;
74   const VkJsonDevice& device2 = instance2.devices.at(0);
75 
76   EXPECT(!memcmp(&device.properties, &device2.properties,
77                  sizeof(device.properties)));
78   for (auto& kv : device.formats) {
79     auto it = device2.formats.find(kv.first);
80     EXPECT(it != device2.formats.end());
81     EXPECT(!memcmp(&kv.second, &it->second, sizeof(kv.second)));
82   }
83 
84   VkImageFormatProperties props = {};
85   json = VkJsonImageFormatPropertiesToJson(props);
86   VkImageFormatProperties props2 = {};
87   result = VkJsonImageFormatPropertiesFromJson(json, &props2, &errors);
88   EXPECT(result);
89   if (!result)
90     std::cout << "Error: " << errors << std::endl;
91 
92   EXPECT(!memcmp(&props, &props2, sizeof(props)));
93 
94   if (g_failures) {
95     std::cout << g_failures << " failures." << std::endl;
96     return 1;
97   } else {
98     std::cout << "Success." << std::endl;
99     return 0;
100   }
101 }
102