vkjson_unittest.cc revision 43b53e83705f02245da6ae61e31273866a35b833
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 36int g_failures; 37 38void ReportFailure(const char* file, int line, const char* assertion) { 39 std::cout << file << ":" << line << ": \"" << assertion << "\" failed." 40 << std::endl; 41 ++g_failures; 42} 43 44int main(int argc, char* argv[]) { 45 std::string errors; 46 bool result = false; 47 48 const char name[] = "Test device"; 49 VkJsonAllProperties device_props; 50 memcpy(device_props.properties.deviceName, name, sizeof(name)); 51 device_props.properties.limits.maxImageDimension1D = 3; 52 device_props.properties.limits.maxSamplerLodBias = 3.5f; 53 device_props.properties.limits.bufferImageGranularity = 0x1ffffffffull; 54 device_props.properties.limits.maxViewportDimensions[0] = 1; 55 device_props.properties.limits.maxViewportDimensions[1] = 2; 56 VkFormatProperties format_props = { 57 VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT, 58 VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT, 59 VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT}; 60 device_props.formats.insert( 61 std::make_pair(VK_FORMAT_R8_UNORM, format_props)); 62 device_props.formats.insert( 63 std::make_pair(VK_FORMAT_R8G8_UNORM, format_props)); 64 std::string json = VkJsonAllPropertiesToJson(device_props); 65 std::cout << json << std::endl; 66 67 VkJsonAllProperties device_props2; 68 result = VkJsonAllPropertiesFromJson(json, &device_props2, &errors); 69 EXPECT(result); 70 if (!result) 71 std::cout << "Error: " << errors << std::endl; 72 73 EXPECT(!memcmp(&device_props.properties, 74 &device_props2.properties, 75 sizeof(device_props.properties))); 76 for (auto& kv : device_props.formats) { 77 auto it = device_props2.formats.find(kv.first); 78 EXPECT(it != device_props2.formats.end()); 79 EXPECT(!memcmp(&kv.second, &it->second, sizeof(kv.second))); 80 } 81 82 VkImageFormatProperties props = {0}; 83 json = VkJsonImageFormatPropertiesToJson(props); 84 VkImageFormatProperties props2 = {0}; 85 result = VkJsonImageFormatPropertiesFromJson(json, &props2, &errors); 86 EXPECT(result); 87 if (!result) 88 std::cout << "Error: " << errors << std::endl; 89 90 EXPECT(!memcmp(&props, &props2, sizeof(props))); 91 92 if (g_failures) { 93 std::cout << g_failures << " failures." << std::endl; 94 return 1; 95 } else { 96 std::cout << "Success." << std::endl; 97 return 0; 98 } 99} 100