onnxruntime/onnxruntime/test/shared_lib/test_model_loading.cc
Scott McKay b5c2932ae8
Last major set of ORT format model changes (#5056)
* Add minimal build option to build.py
Group some of the build settings so binary size reduction options are all together
Make some cmake variable naming more consistent
Replace usage of std::hash with murmurhash3 for kernel. std::hash is implementation dependent so can't be used.
Add initial doco and ONNX to ORT model conversion script
Misc cleanups of minimal build breaks.
2020-09-05 07:59:01 +10:00

61 lines
1.8 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "core/session/onnxruntime_cxx_api.h"
#ifdef USE_CUDA
#include "core/providers/cuda/cuda_provider_factory.h"
#endif
#include <fstream>
#include "test_fixture.h"
#include "file_util.h"
#include "gmock/gmock.h"
extern std::unique_ptr<Ort::Env> ort_env;
namespace onnxruntime {
namespace test {
// disable for minimal build with no exceptions as it will always attempt to throw in that scenario
#if !defined(ORT_MINIMAL_BUILD) && !defined(ORT_NO_EXCEPTIONS)
TEST(CApiTest, model_from_array) {
const char* model_path = "testdata/matmul_1.onnx";
std::vector<char> buffer;
{
std::ifstream file(model_path, std::ios::binary | std::ios::ate);
if (!file)
ORT_THROW("Error reading model");
buffer.resize(file.tellg());
file.seekg(0, std::ios::beg);
if (!file.read(buffer.data(), buffer.size()))
ORT_THROW("Error reading model");
}
#if (!ORT_MINIMAL_BUILD)
bool should_throw = false;
#else
bool should_throw = true;
#endif
auto create_session = [&](Ort::SessionOptions& so) {
try {
Ort::Session session(*ort_env.get(), buffer.data(), buffer.size(), so);
ASSERT_FALSE(should_throw) << "Creation of session should have thrown";
} catch (const std::exception& ex) {
ASSERT_TRUE(should_throw) << "Creation of session should not have thrown. Exception:" << ex.what();
ASSERT_THAT(ex.what(), testing::HasSubstr("ONNX format model is not supported in this build."));
}
};
Ort::SessionOptions so;
create_session(so);
#ifdef USE_CUDA
// test with CUDA provider when using onnxruntime as dll
Ort::ThrowOnError(OrtSessionOptionsAppendExecutionProvider_CUDA(so, 0));
create_session(so);
#endif
}
#endif
} // namespace test
} // namespace onnxruntime