Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_stack_test.cc « blenlib « gtests « tests - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ad4d95781343df3f16655bef7db956caebd522f (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
130
131
132
133
134
135
136
137
/* Apache License, Version 2.0 */

#include "testing/testing.h"
#include <string.h>

extern "C" {
#include "BLI_stack.h"
#include "BLI_utildefines.h"
#include "BLI_array.h"
};

#define SIZE 1024

TEST(stack, Empty)
{
	BLI_Stack *stack;

	stack = BLI_stack_new(sizeof(int), __func__);
	EXPECT_EQ(BLI_stack_is_empty(stack), true);
	BLI_stack_free(stack);
}

TEST(stack, One)
{
	BLI_Stack *stack;
	unsigned int in = -1, out = 1;

	stack = BLI_stack_new(sizeof(in), __func__);

	BLI_stack_push(stack, (void *)&in);
	EXPECT_EQ(BLI_stack_is_empty(stack), false);
	BLI_stack_pop(stack, (void *)&out);
	EXPECT_EQ(in, out);
	EXPECT_EQ(BLI_stack_is_empty(stack), true);
	BLI_stack_free(stack);
}

TEST(stack, Range)
{
	const int tot = SIZE;
	BLI_Stack *stack;
	int in, out;

	stack = BLI_stack_new(sizeof(in), __func__);

	for (in = 0; in < tot; in++) {
		BLI_stack_push(stack, (void *)&in);
	}

	for (in = tot - 1; in >= 0; in--) {
		EXPECT_EQ(BLI_stack_is_empty(stack), false);
		BLI_stack_pop(stack, (void *)&out);
		EXPECT_EQ(in, out);

	}
	EXPECT_EQ(BLI_stack_is_empty(stack), true);

	BLI_stack_free(stack);
}

TEST(stack, String)
{
	const int tot = SIZE;
	int i;

	BLI_Stack *stack;
	char in[] = "hello world!";
	char out[sizeof(in)];

	stack = BLI_stack_new(sizeof(in), __func__);

	for (i = 0; i < tot; i++) {
		*((int *)in) = i;
		BLI_stack_push(stack, (void *)in);
	}

	for (i = tot - 1; i >= 0; i--) {
		EXPECT_EQ(BLI_stack_is_empty(stack), false);
		*((int *)in) = i;
		BLI_stack_pop(stack, (void *)&out);
		EXPECT_STREQ(in, out);

	}
	EXPECT_EQ(BLI_stack_is_empty(stack), true);

	BLI_stack_free(stack);
}

TEST(stack, Reuse)
{
	const int sizes[] = {3, 11, 81, 400, 999, 12, 1, 9721, 7, 99, 5, 0};
	int sizes_test[ARRAY_SIZE(sizes)];
	const int *s;
	int in, out, i;
	int sum, sum_test;

	BLI_Stack *stack;

	stack = BLI_stack_new(sizeof(in), __func__);

	/* add a bunch of numbers, ensure we get same sum out */
	sum = 0;
	for (s = sizes; *s; s++) {
		for (i = *s; i != 0; i--) {
			BLI_stack_push(stack, (void *)&i);
			sum += i;
		}
	}
	sum_test = 0;
	while (!BLI_stack_is_empty(stack)) {
		BLI_stack_pop(stack, (void *)&out);
		sum_test += out;
	}
	EXPECT_EQ(sum, sum_test);

	/* add and remove all except last */
	for (s = sizes; *s; s++) {
		for (i = *s; i >= 0; i--) {
			BLI_stack_push(stack, (void *)&i);
		}
		for (i = *s; i > 0; i--) {
			BLI_stack_pop(stack, (void *)&out);
		}
	}

	i = ARRAY_SIZE(sizes) - 1;
	while (!BLI_stack_is_empty(stack)) {
		i--;
		BLI_stack_pop(stack, (void *)&sizes_test[i]);
		EXPECT_EQ(sizes[i], sizes_test[i]);
		EXPECT_GT(i, -1);
	}
	EXPECT_EQ(i, 0);
	EXPECT_EQ(memcmp(sizes, sizes_test, sizeof(sizes) - sizeof(int)), 0);

	BLI_stack_free(stack);
}