Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions tests/testDriver_capability_matrix_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "testsupport.h"

#include <string.h>

/*
* Tests for capability-matrix snapshot API.
*/

void setUp(void) {
memset(&nnpa_query_result, 0, sizeof(nnpa_query_result));
aiu_lib_vernum = 0;
}

void tearDown(void) {}

void test_get_capability_matrix_rejects_null_output(void) {
TEST_ASSERT_EQUAL(ZDNN_INVALID_BUFFER, zdnn_get_capability_matrix(NULL));
}

void test_get_capability_matrix_returns_cached_query_snapshot(void) {
nnpa_query_result.installed_functions_vector.bits_0to63 = 0x0123456789ABCDEF;
nnpa_query_result.installed_functions_vector.bits_64to127 =
0x89ABCDEF01234567;
nnpa_query_result.installed_functions_vector.bits_128to191 =
0xAAAAAAAA55555555;
nnpa_query_result.installed_functions_vector.bits_192to255 =
0x13579BDF2468ACE0;

nnpa_query_result.installed_parameter_block_formats.bits_0to63 =
0x00FF00FF00FF00FF;
nnpa_query_result.installed_parameter_block_formats.bits_64to127 =
0x0F0F0F0F0F0F0F0F;

nnpa_query_result.installed_data_types = 0xA5A5;
nnpa_query_result.installed_dt1_conversions_vector = 0x5A5A;
nnpa_query_result.installed_data_layout_formats = 0xCC33CC33;
nnpa_query_result.maximum_dimension_index_size = 32768;
nnpa_query_result.max_dim4_index_size = 16384;
nnpa_query_result.max_dim3_index_size = 8192;
nnpa_query_result.max_dim2_index_size = 4096;
nnpa_query_result.max_dim1_index_size = 2048;
nnpa_query_result.maximum_tensor_size = 0x0000000180000000ULL;

aiu_lib_vernum = 0x01020A;

zdnn_capability_matrix caps;
memset(&caps, 0, sizeof(caps));

TEST_ASSERT_EQUAL(ZDNN_OK, zdnn_get_capability_matrix(&caps));

TEST_ASSERT_EQUAL_HEX64(0x0123456789ABCDEFULL, caps.installed_functions[0]);
TEST_ASSERT_EQUAL_HEX64(0x89ABCDEF01234567ULL, caps.installed_functions[1]);
TEST_ASSERT_EQUAL_HEX64(0xAAAAAAAA55555555ULL, caps.installed_functions[2]);
TEST_ASSERT_EQUAL_HEX64(0x13579BDF2468ACE0ULL, caps.installed_functions[3]);

TEST_ASSERT_EQUAL_HEX64(0x00FF00FF00FF00FFULL, caps.installed_parmblk_fmts[0]);
TEST_ASSERT_EQUAL_HEX64(0x0F0F0F0F0F0F0F0FULL, caps.installed_parmblk_fmts[1]);

TEST_ASSERT_EQUAL_HEX16(0xA5A5, caps.installed_data_types);
TEST_ASSERT_EQUAL_HEX16(0x5A5A, caps.installed_dt1_conversions);
TEST_ASSERT_EQUAL_HEX32(0xCC33CC33, caps.installed_data_layout_formats);

TEST_ASSERT_EQUAL_UINT32(32768, caps.maximum_dimension_index_size);
TEST_ASSERT_EQUAL_UINT32(16384, caps.max_dim4_index_size);
TEST_ASSERT_EQUAL_UINT32(8192, caps.max_dim3_index_size);
TEST_ASSERT_EQUAL_UINT32(4096, caps.max_dim2_index_size);
TEST_ASSERT_EQUAL_UINT32(2048, caps.max_dim1_index_size);
TEST_ASSERT_EQUAL_HEX64(0x0000000180000000ULL, caps.maximum_tensor_size);
TEST_ASSERT_EQUAL_HEX32(0x01020A, caps.aiu_library_version);
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_get_capability_matrix_rejects_null_output);
RUN_TEST(test_get_capability_matrix_returns_cached_query_snapshot);

return UNITY_END();
}
110 changes: 110 additions & 0 deletions tests/testDriver_query_count_safety.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "testsupport.h"

#include <limits.h>
#include <stdint.h>
#include <string.h>

/*
* Regression tests for query varargs count handling.
*
* Before this hardening, these loops used small unsigned counters (uint16_t
* and uint8_t) against an int `count`. Very large count values could cause
* counter wraparound and non-terminating loops.
*/

void setUp(void) {
memset(&nnpa_query_result, 0, sizeof(nnpa_query_result));

// Mark all functions/formats as installed so negative checks in this file are
// specifically validating count/input guards.
nnpa_query_result.installed_functions_vector.bits_0to63 = UINT64_MAX;
nnpa_query_result.installed_functions_vector.bits_64to127 = UINT64_MAX;
nnpa_query_result.installed_functions_vector.bits_128to191 = UINT64_MAX;
nnpa_query_result.installed_functions_vector.bits_192to255 = UINT64_MAX;
nnpa_query_result.installed_parameter_block_formats.bits_0to63 = UINT64_MAX;
nnpa_query_result.installed_parameter_block_formats.bits_64to127 =
UINT64_MAX;
}

void tearDown(void) {}

void test_function_query_rejects_non_positive_count(void) {
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed(0));
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed(-1));
}

void test_function_query_rejects_count_above_bitmap_capacity(void) {
int too_many =
(int)BIT_SIZEOF(nnpa_query_result.installed_functions_vector) + 1;
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed(too_many));
}

void test_function_query_rejects_extreme_count(void) {
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed(INT_MAX));
}

void test_function_query_rejects_negative_function_code(void) {
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed(1, -1));
}

void test_function_query_valid_input_still_succeeds(void) {
TEST_ASSERT_TRUE(zdnn_is_nnpa_function_installed(2, NNPA_ADD, NNPA_SOFTMAX));
}

void test_parmblk_query_rejects_non_positive_count(void) {
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed(0));
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed(-1));
}

void test_parmblk_query_rejects_count_above_bitmap_capacity(void) {
int too_many =
(int)BIT_SIZEOF(nnpa_query_result.installed_parameter_block_formats) + 1;
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed(too_many));
}

void test_parmblk_query_rejects_extreme_count(void) {
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed(INT_MAX));
}

void test_parmblk_query_rejects_negative_format_code(void) {
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed(1, -1));
}

void test_parmblk_query_valid_input_still_succeeds(void) {
TEST_ASSERT_TRUE(
zdnn_is_nnpa_parmblk_fmt_installed(1, NNPA_PARMBLKFORMAT_0));
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_function_query_rejects_non_positive_count);
RUN_TEST(test_function_query_rejects_count_above_bitmap_capacity);
RUN_TEST(test_function_query_rejects_extreme_count);
RUN_TEST(test_function_query_rejects_negative_function_code);
RUN_TEST(test_function_query_valid_input_still_succeeds);
RUN_TEST(test_parmblk_query_rejects_non_positive_count);
RUN_TEST(test_parmblk_query_rejects_count_above_bitmap_capacity);
RUN_TEST(test_parmblk_query_rejects_extreme_count);
RUN_TEST(test_parmblk_query_rejects_negative_format_code);
RUN_TEST(test_parmblk_query_valid_input_still_succeeds);

return UNITY_END();
}
124 changes: 124 additions & 0 deletions tests/testDriver_query_typed_wrappers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright IBM Corp. 2024
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "testsupport.h"

#include <stdint.h>
#include <string.h>

void setUp(void) {
memset(&nnpa_query_result, 0, sizeof(nnpa_query_result));

// Start with all bits installed so each test can remove only what it needs.
nnpa_query_result.installed_functions_vector.bits_0to63 = UINT64_MAX;
nnpa_query_result.installed_functions_vector.bits_64to127 = UINT64_MAX;
nnpa_query_result.installed_functions_vector.bits_128to191 = UINT64_MAX;
nnpa_query_result.installed_functions_vector.bits_192to255 = UINT64_MAX;
nnpa_query_result.installed_parameter_block_formats.bits_0to63 = UINT64_MAX;
nnpa_query_result.installed_parameter_block_formats.bits_64to127 =
UINT64_MAX;
}

void tearDown(void) {}

void test_function_list_rejects_null_or_zero_count(void) {
nnpa_function_code funcs[] = {NNPA_ADD};

TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed_list(NULL, 1));
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed_list(funcs, 0));
}

void test_function_list_rejects_invalid_entries(void) {
nnpa_function_code invalid_negative[] = {(nnpa_function_code)-1};
nnpa_function_code invalid_large[] = {(nnpa_function_code)999};

TEST_ASSERT_FALSE(
zdnn_is_nnpa_function_installed_list(invalid_negative, 1));
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed_list(invalid_large, 1));
}

void test_function_list_matches_varargs_for_valid_input(void) {
nnpa_function_code funcs[] = {NNPA_ADD, NNPA_SOFTMAX, NNPA_TRANSFORM};

bool typed = zdnn_is_nnpa_function_installed_list(funcs, 3);
bool variadic = zdnn_is_nnpa_function_installed(3, NNPA_ADD, NNPA_SOFTMAX,
NNPA_TRANSFORM);

TEST_ASSERT_TRUE(typed);
TEST_ASSERT_EQUAL(variadic, typed);
}

void test_function_list_reports_missing_function(void) {
nnpa_query_result.installed_functions_vector.bits_0to63 = 0;
nnpa_query_result.installed_functions_vector.bits_64to127 = 0;
nnpa_query_result.installed_functions_vector.bits_128to191 = 0;
nnpa_query_result.installed_functions_vector.bits_192to255 = 0;

nnpa_function_code funcs[] = {NNPA_ADD};
TEST_ASSERT_FALSE(zdnn_is_nnpa_function_installed_list(funcs, 1));
}

void test_parmblk_list_rejects_null_or_zero_count(void) {
nnpa_parmblk_format formats[] = {NNPA_PARMBLKFORMAT_0};

TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed_list(NULL, 1));
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed_list(formats, 0));
}

void test_parmblk_list_rejects_invalid_entries(void) {
nnpa_parmblk_format invalid_negative[] = {(nnpa_parmblk_format)-1};
nnpa_parmblk_format invalid_large[] = {(nnpa_parmblk_format)999};

TEST_ASSERT_FALSE(
zdnn_is_nnpa_parmblk_fmt_installed_list(invalid_negative, 1));
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed_list(invalid_large, 1));
}

void test_parmblk_list_matches_varargs_for_valid_input(void) {
nnpa_parmblk_format formats[] = {NNPA_PARMBLKFORMAT_0, NNPA_PARMBLKFORMAT_1};

bool typed = zdnn_is_nnpa_parmblk_fmt_installed_list(formats, 2);
bool variadic =
zdnn_is_nnpa_parmblk_fmt_installed(2, NNPA_PARMBLKFORMAT_0,
NNPA_PARMBLKFORMAT_1);

TEST_ASSERT_TRUE(typed);
TEST_ASSERT_EQUAL(variadic, typed);
}

void test_parmblk_list_reports_missing_format(void) {
nnpa_query_result.installed_parameter_block_formats.bits_0to63 = 0;
nnpa_query_result.installed_parameter_block_formats.bits_64to127 = 0;

nnpa_parmblk_format formats[] = {NNPA_PARMBLKFORMAT_0};
TEST_ASSERT_FALSE(zdnn_is_nnpa_parmblk_fmt_installed_list(formats, 1));
}

int main(void) {
UNITY_BEGIN();

RUN_TEST(test_function_list_rejects_null_or_zero_count);
RUN_TEST(test_function_list_rejects_invalid_entries);
RUN_TEST(test_function_list_matches_varargs_for_valid_input);
RUN_TEST(test_function_list_reports_missing_function);
RUN_TEST(test_parmblk_list_rejects_null_or_zero_count);
RUN_TEST(test_parmblk_list_rejects_invalid_entries);
RUN_TEST(test_parmblk_list_matches_varargs_for_valid_input);
RUN_TEST(test_parmblk_list_reports_missing_format);

return UNITY_END();
}
Loading