vkjson_unittest.cc revision 594091db7f508f6073975efddb5fab8d0fb273e7
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  const char name[] = "Test device";
56  VkJsonDevice device;
57  memcpy(device.properties.deviceName, name, sizeof(name));
58  device.properties.limits.maxImageDimension1D = 3;
59  device.properties.limits.maxSamplerLodBias = 3.5f;
60  device.properties.limits.bufferImageGranularity = 0x1ffffffffull;
61  device.properties.limits.maxViewportDimensions[0] = 1;
62  device.properties.limits.maxViewportDimensions[1] = 2;
63  VkFormatProperties format_props = {
64      VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT,
65      VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT,
66      VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT};
67  device.formats.insert(std::make_pair(VK_FORMAT_R8_UNORM, format_props));
68  device.formats.insert(std::make_pair(VK_FORMAT_R8G8_UNORM, format_props));
69  std::string json = VkJsonDeviceToJson(device);
70  std::cout << json << std::endl;
71
72  VkJsonDevice device2;
73  result = VkJsonDeviceFromJson(json, &device2, &errors);
74  EXPECT(result);
75  if (!result)
76    std::cout << "Error: " << errors << std::endl;
77
78  EXPECT(!memcmp(&device.properties, &device2.properties,
79                 sizeof(device.properties)));
80  for (auto& kv : device.formats) {
81    auto it = device2.formats.find(kv.first);
82    EXPECT(it != device2.formats.end());
83    EXPECT(!memcmp(&kv.second, &it->second, sizeof(kv.second)));
84  }
85
86  VkImageFormatProperties props = {0};
87  json = VkJsonImageFormatPropertiesToJson(props);
88  VkImageFormatProperties props2 = {0};
89  result = VkJsonImageFormatPropertiesFromJson(json, &props2, &errors);
90  EXPECT(result);
91  if (!result)
92    std::cout << "Error: " << errors << std::endl;
93
94  EXPECT(!memcmp(&props, &props2, sizeof(props)));
95
96  if (g_failures) {
97    std::cout << g_failures << " failures." << std::endl;
98    return 1;
99  } else {
100    std::cout << "Success." << std::endl;
101    return 0;
102  }
103}
104