1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2019 Google, LLC 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "util/u_vector.h" 25bf215546Sopenharmony_ci#include "gtest/gtest.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_cistatic void test(uint32_t size_in_elements, uint32_t elements_to_walk, uint32_t start) 28bf215546Sopenharmony_ci{ 29bf215546Sopenharmony_ci struct u_vector vector; 30bf215546Sopenharmony_ci uint32_t add_counter = 0; 31bf215546Sopenharmony_ci uint32_t remove_counter = 0; 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_ci ASSERT_TRUE(u_vector_init(&vector, size_in_elements, sizeof(uint64_t))); 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci // Override the head and tail so we can quickly test rollover 36bf215546Sopenharmony_ci vector.head = vector.tail = start; 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci EXPECT_EQ(sizeof(uint64_t) * size_in_elements, vector.size); 39bf215546Sopenharmony_ci EXPECT_EQ(0, u_vector_length(&vector)); 40bf215546Sopenharmony_ci 41bf215546Sopenharmony_ci for (uint32_t i = 0; i < size_in_elements; i++) { 42bf215546Sopenharmony_ci *(uint64_t*)u_vector_add(&vector) = add_counter++; 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci int length = u_vector_length(&vector); 45bf215546Sopenharmony_ci EXPECT_EQ(i + 1, length); 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci // Check the entries 48bf215546Sopenharmony_ci uint32_t count = 0; 49bf215546Sopenharmony_ci void* element; 50bf215546Sopenharmony_ci u_vector_foreach(element, &vector) 51bf215546Sopenharmony_ci { 52bf215546Sopenharmony_ci EXPECT_EQ(count, *(uint64_t*)element) << "i: " << i << " count: " << count; 53bf215546Sopenharmony_ci count++; 54bf215546Sopenharmony_ci } 55bf215546Sopenharmony_ci EXPECT_EQ(count, length); 56bf215546Sopenharmony_ci } 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci // Remove + add 59bf215546Sopenharmony_ci for (uint32_t i = 0; i < elements_to_walk; i++) { 60bf215546Sopenharmony_ci u_vector_remove(&vector); 61bf215546Sopenharmony_ci remove_counter++; 62bf215546Sopenharmony_ci *(uint64_t*)u_vector_add(&vector) = add_counter++; 63bf215546Sopenharmony_ci } 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci EXPECT_EQ(sizeof(uint64_t) * size_in_elements, vector.size); 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci // Grow the vector now 68bf215546Sopenharmony_ci *(uint64_t*)u_vector_add(&vector) = add_counter++; 69bf215546Sopenharmony_ci EXPECT_EQ(size_in_elements + 1, u_vector_length(&vector)); 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci EXPECT_EQ(sizeof(uint64_t) * size_in_elements * 2, vector.size); 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_ci { 74bf215546Sopenharmony_ci uint32_t count = remove_counter; 75bf215546Sopenharmony_ci void* element; 76bf215546Sopenharmony_ci u_vector_foreach(element, &vector) 77bf215546Sopenharmony_ci { 78bf215546Sopenharmony_ci EXPECT_EQ(count++, *(uint64_t*)element) << "count: " << count; 79bf215546Sopenharmony_ci } 80bf215546Sopenharmony_ci } 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci u_vector_finish(&vector); 83bf215546Sopenharmony_ci} 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ciTEST(Vector, Grow0) { test(4, 0, 0); } 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ciTEST(Vector, Grow1) { test(4, 1, 0); } 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ciTEST(Vector, Grow2) { test(4, 2, 0); } 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ciTEST(Vector, Grow3) { test(4, 3, 0); } 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ciTEST(Vector, Grow4) { test(4, 4, 0); } 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ciTEST(Vector, Grow5) { test(4, 5, 0); } 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ciTEST(Vector, Rollover) 98bf215546Sopenharmony_ci{ 99bf215546Sopenharmony_ci uint32_t start = (1ull << 32) - 4 * sizeof(uint64_t); 100bf215546Sopenharmony_ci test(8, 4, start); 101bf215546Sopenharmony_ci} 102