summaryrefslogtreecommitdiff
path: root/test/std_vector.cc
blob: 1d84e4385ddc7e54a1dde18a7cce7f837c1dcd2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Basic test of std::vector and dynamic memory allocation.
 */

#include <array>
#include <vector>

extern "C" {
#include "common.h"
#include "console.h"
#include "test_util.h"
}

test_static int stack_init_elements()
{
	std::vector<int32_t> vec{ 10, 11, 12, 13, 14 };

	TEST_EQ(static_cast<int32_t>(vec.size()), 5, "%d");
	TEST_EQ(vec[0], 10, "%d");
	TEST_EQ(vec[1], 11, "%d");
	TEST_EQ(vec[2], 12, "%d");
	TEST_EQ(vec[3], 13, "%d");
	TEST_EQ(vec[4], 14, "%d");

	return EC_SUCCESS;
}

test_static int push_back_elements()
{
	std::vector<int32_t> vec;

	vec.push_back(0);
	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);

	TEST_EQ(static_cast<int32_t>(vec.size()), 4, "%d");
	TEST_EQ(vec[0], 0, "%d");
	TEST_EQ(vec[1], 1, "%d");
	TEST_EQ(vec[2], 2, "%d");
	TEST_EQ(vec[3], 3, "%d");

	return EC_SUCCESS;
}

test_static int fill_one_vector()
{
	// This test allocates 64kB of memory in total in a single std::vector
	constexpr int num_elements = 16 * 1024;
	std::vector<int32_t> vec;

	for (int i = 0; i < num_elements; ++i)
		vec.push_back(i);

	TEST_EQ(static_cast<int>(vec.size()), num_elements, "%d");
	for (int i = 0; i < num_elements; ++i) {
		TEST_ASSERT(vec[i] == i);
		// Using TEST_EQ floods the console and trigger the watchdog
		// TEST_EQ(vec[i], i, "%d");
		// cflush();
	}

	return EC_SUCCESS;
}

test_static int fill_multiple_vectors()
{
	// This test allocates 64kB of memory in total split in 8 std::vectors
	constexpr int num_elements = 2 * 1024;
	std::array<std::vector<int32_t>, 8> vecs;

	for (int i = 0; i < num_elements; ++i)
		for (auto &vec : vecs)
			vec.push_back(i);

	for (auto &vec : vecs) {
		TEST_EQ(static_cast<int>(vec.size()), num_elements, "%d");
		for (int i = 0; i < num_elements; ++i) {
			TEST_ASSERT(vec[i] == i);
		}
	}

	return EC_SUCCESS;
}

test_static int create_and_destroy_two_vectors()
{
	// This allocates 64kB of memory twice.
	// The first vector is declared in a local scope and the memory is
	// free'd at the end of the block.
	constexpr int num_elements = 16 * 1024;
	{
		std::vector<int32_t> vec;
		for (int i = 0; i < num_elements; ++i)
			vec.push_back(i);

		TEST_EQ(static_cast<int>(vec.size()), num_elements, "%d");
		for (int i = 0; i < num_elements; ++i) {
			TEST_ASSERT(vec[i] == i);
		}
	}

	std::vector<int32_t> vec;
	for (int i = 0; i < num_elements; ++i)
		vec.push_back(i);

	TEST_EQ(static_cast<int>(vec.size()), num_elements, "%d");
	for (int i = 0; i < num_elements; ++i) {
		TEST_ASSERT(vec[i] == i);
	}

	return EC_SUCCESS;
}

extern "C" void run_test(int argc, const char **argv)
{
	test_reset();

	RUN_TEST(stack_init_elements);
	RUN_TEST(push_back_elements);
	RUN_TEST(fill_one_vector);
	RUN_TEST(fill_multiple_vectors);
	RUN_TEST(create_and_destroy_two_vectors);

	test_print_result();
}