|
1 | 1 | /** |
2 | | -* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2014. ALL RIGHTS RESERVED. |
| 2 | +* Copyright (c) NVIDIA CORPORATION & AFFILIATES, 2001-2026. ALL RIGHTS RESERVED. |
3 | 3 | * Copyright (C) UT-Battelle, LLC. 2014. ALL RIGHTS RESERVED. |
4 | 4 | * Copyright (C) Huawei Technologies Co., Ltd. 2020. ALL RIGHTS RESERVED. |
5 | 5 | * See file LICENSE for terms. |
@@ -1115,6 +1115,50 @@ class test_array : public test_datatype { |
1115 | 1115 | void cleanup_array_of_linked_lists(test_list_links_array_t *test_array); |
1116 | 1116 | }; |
1117 | 1117 |
|
| 1118 | +UCS_TEST_F(test_array, carray_for_each_index) { |
| 1119 | + const int values[] = {3, 5, 7}; |
| 1120 | + const int *elem; |
| 1121 | + size_t idx; |
| 1122 | + |
| 1123 | + ucs_carray_for_each_index(elem, idx, values, 0) { |
| 1124 | + ADD_FAILURE() << "iterated over an empty array"; |
| 1125 | + } |
| 1126 | + EXPECT_EQ(0, idx); |
| 1127 | + |
| 1128 | + ucs_carray_for_each_index(elem, idx, values, |
| 1129 | + ucs_static_array_size(values)) { |
| 1130 | + EXPECT_EQ(&values[idx], elem); |
| 1131 | + EXPECT_EQ(values[idx], *elem); |
| 1132 | + } |
| 1133 | + EXPECT_EQ(ucs_static_array_size(values), idx); |
| 1134 | +} |
| 1135 | + |
| 1136 | +UCS_TEST_F(test_array, array_for_each_index) { |
| 1137 | + constexpr size_t NUM_ELEMENTS = 3; |
| 1138 | + test_1int_t test_array; |
| 1139 | + int *elem; |
| 1140 | + size_t idx; |
| 1141 | + |
| 1142 | + ucs_array_init_dynamic(&test_array); |
| 1143 | + |
| 1144 | + ucs_array_for_each_index(elem, idx, &test_array) { |
| 1145 | + ADD_FAILURE() << "iterated over an empty array"; |
| 1146 | + } |
| 1147 | + EXPECT_EQ(0, idx); |
| 1148 | + |
| 1149 | + for (size_t i = 0; i < NUM_ELEMENTS; ++i) { |
| 1150 | + *ucs_array_append(&test_array, FAIL()) = static_cast<int>(i * i + 1); |
| 1151 | + } |
| 1152 | + |
| 1153 | + ucs_array_for_each_index(elem, idx, &test_array) { |
| 1154 | + EXPECT_EQ(&ucs_array_elem(&test_array, idx), elem); |
| 1155 | + EXPECT_EQ(idx * idx + 1, static_cast<size_t>(*elem)); |
| 1156 | + } |
| 1157 | + EXPECT_EQ(NUM_ELEMENTS, idx); |
| 1158 | + |
| 1159 | + ucs_array_cleanup_dynamic(&test_array); |
| 1160 | +} |
| 1161 | + |
1118 | 1162 | /* generate a list of numbers in a certain size */ |
1119 | 1163 | void test_array::generate_linked_list(int size, simple_elem_t *head) |
1120 | 1164 | { |
|
0 commit comments