// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "gtest/gtest.h" #include "test/providers/provider_test_utils.h" using namespace std; namespace onnxruntime { namespace test { template void TestHelper(std::initializer_list classes_init, const std::string& type, const vector& input_dims, OpTester::ExpectResult expect_result = OpTester::ExpectResult::kExpectSuccess) { // View the class list as a non-owning span rather than copying it into a std::vector. The braced // list at each call site is a std::initializer_list parameter (stack-backed), so there is no // vector temporary in the inlined TEST body for GCC 15 to false-positive on with // -Wfree-nonheap-object. The backing array outlives this call, so the span stays valid through // test.Run() below, which is where the deferred AddAttribute reads it. const gsl::span classes(classes_init.begin(), classes_init.size()); OpTester test("ZipMap", 1, onnxruntime::kMLDomain); std::vector input{1.f, 0.f, 3.f, 44.f, 23.f, 11.3f}; if (type == "string") { test.AddAttribute("classlabels_strings", classes); } else if (type == "int64_t") { test.AddAttribute("classlabels_int64s", classes); } else { ORT_THROW("Invalid type: ", type); } int64_t batch_size = (input_dims.size() > 1) ? input_dims[0] : 1; // prepare expected output std::vector> expected_output; if (expect_result == OpTester::ExpectResult::kExpectSuccess) { for (int64_t i = 0; i < batch_size; ++i) { std::map var_map; for (size_t j = 0; j < classes.size(); ++j) { var_map.emplace(classes[j], input[i * 3 + j]); } expected_output.push_back(var_map); } } test.AddInput("X", input_dims, input); test.AddOutput("Z", expected_output); test.Run(expect_result); } // Positive test cases TEST(MLOpTest, ZipMapOpStringFloat) { TestHelper({"class1", "class2", "class3"}, "string", {2, 3}); } TEST(MLOpTest, ZipMapOpInt64Float) { TestHelper({10, 20, 30}, "int64_t", {2, 3}); } TEST(MLOpTest, ZipMapOpInt64Float1D) { TestHelper({10, 20, 30, 40, 50, 60}, "int64_t", {6}); } // Negative test cases TEST(MLOpTest, ZipMapOpStringFloatStrideMoreThanNumLabels) { TestHelper({"class1", "class2", "class3"}, "string", {1, 6}, OpTester::ExpectResult::kExpectFailure); } TEST(MLOpTest, ZipMapOpStringFloatStrideLessThanNumLabels) { TestHelper({"class1", "class2", "class3"}, "string", {3, 2}, OpTester::ExpectResult::kExpectFailure); } TEST(MLOpTest, ZipMapOpInt64FloatStrideMoreThanNumLabels) { TestHelper({10, 20, 30}, "int64_t", {1, 6}, OpTester::ExpectResult::kExpectFailure); } TEST(MLOpTest, ZipMapOpInt64FloatStrideLessThanNumLabels) { TestHelper({10, 20, 30}, "int64_t", {3, 2}, OpTester::ExpectResult::kExpectFailure); } } // namespace test } // namespace onnxruntime