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