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// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and/or associated documentation files (the "Materials"), to
10// deal in the Materials without restriction, including without limitation the
11// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12// sell copies of the Materials, and to permit persons to whom the Materials are
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice(s) and this permission notice shall be included in
16// all copies or substantial portions of the Materials.
17//
18// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21//
22// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25// USE OR OTHER DEALINGS IN THE MATERIALS.
26///////////////////////////////////////////////////////////////////////////////
27
28#include "vkjson.h"
29
30#include <stdlib.h>
31#include <string.h>
32
33#include <iostream>
34
35#define EXPECT(X) if (!(X)) \
36  ReportFailure(__FILE__, __LINE__, #X);
37
38#define ASSERT(X) if (!(X)) { \
39  ReportFailure(__FILE__, __LINE__, #X); \
40  return 2; \
41}
42
43int g_failures;
44
45void ReportFailure(const char* file, int line, const char* assertion) {
46  std::cout << file << ":" << line << ": \"" << assertion << "\" failed."
47            << std::endl;
48  ++g_failures;
49}
50
51int main(int argc, char* argv[]) {
52  std::string errors;
53  bool result = false;
54
55  VkJsonInstance instance;
56  instance.devices.resize(1);
57  VkJsonDevice& device = instance.devices[0];
58
59  const char name[] = "Test device";
60  memcpy(device.properties.deviceName, name, sizeof(name));
61  device.properties.limits.maxImageDimension1D = 3;
62  device.properties.limits.maxSamplerLodBias = 3.5f;
63  device.properties.limits.bufferImageGranularity = 0x1ffffffffull;
64  device.properties.limits.maxViewportDimensions[0] = 1;
65  device.properties.limits.maxViewportDimensions[1] = 2;
66  VkFormatProperties format_props = {
67      VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT,
68      VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT,
69      VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT};
70  device.formats.insert(std::make_pair(VK_FORMAT_R8_UNORM, format_props));
71  device.formats.insert(std::make_pair(VK_FORMAT_R8G8_UNORM, format_props));
72
73  std::string json = VkJsonInstanceToJson(instance);
74  std::cout << json << std::endl;
75
76  VkJsonInstance instance2;
77  result = VkJsonInstanceFromJson(json, &instance2, &errors);
78  EXPECT(result);
79  if (!result)
80    std::cout << "Error: " << errors << std::endl;
81  const VkJsonDevice& device2 = instance2.devices.at(0);
82
83  EXPECT(!memcmp(&device.properties, &device2.properties,
84                 sizeof(device.properties)));
85  for (auto& kv : device.formats) {
86    auto it = device2.formats.find(kv.first);
87    EXPECT(it != device2.formats.end());
88    EXPECT(!memcmp(&kv.second, &it->second, sizeof(kv.second)));
89  }
90
91  VkImageFormatProperties props = {0};
92  json = VkJsonImageFormatPropertiesToJson(props);
93  VkImageFormatProperties props2 = {0};
94  result = VkJsonImageFormatPropertiesFromJson(json, &props2, &errors);
95  EXPECT(result);
96  if (!result)
97    std::cout << "Error: " << errors << std::endl;
98
99  EXPECT(!memcmp(&props, &props2, sizeof(props)));
100
101  if (g_failures) {
102    std::cout << g_failures << " failures." << std::endl;
103    return 1;
104  } else {
105    std::cout << "Success." << std::endl;
106    return 0;
107  }
108}
109