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