64 lines
2.1 KiB
C
64 lines
2.1 KiB
C
/*
|
|
vector_altcap.c - Probes vector.h with a non-default DLIBC_VECTOR_INITIAL_CAPACITY.
|
|
|
|
The macro is read at include time, so exercising it needs a translation unit of
|
|
its own. Every function in vector.h is static inline, so this TU gets its own
|
|
copies built around the smaller constant while the vector_t layout stays
|
|
identical to the one in test_vector.c. That means a vector created here is a
|
|
perfectly ordinary vector to the rest of the program.
|
|
|
|
@attention This TU deliberately does not include dtest.h. The harness counters
|
|
are file-scope statics, so an assertion made here would be tallied separately
|
|
and never reach the summary. The probes below only gather observations, and
|
|
test_vector.c does the asserting.
|
|
*/
|
|
|
|
#define DLIBC_VECTOR_INITIAL_CAPACITY 1
|
|
#include "../vector.h"
|
|
|
|
/*
|
|
@brief Records the vector's capacity after each of count successive push_back() calls, starting from a freshly created vector.
|
|
@param out A buffer of at least count size_t values, filled with the capacity observed after each push.
|
|
@param count The number of pushes to perform.
|
|
@return 0 on success, -1 if out is NULL, count is 0, or any allocation fails.
|
|
@attention With DLIBC_VECTOR_INITIAL_CAPACITY at 1, the expected sequence is 1, 2, 4, 4, 8, 8, 8, 8, ...
|
|
*/
|
|
int vector_altcap_growth_sequence(size_t* out, size_t count) {
|
|
if (!out || count == 0) {
|
|
return -1;
|
|
}
|
|
|
|
vector_t* vec = vector_create(sizeof(int));
|
|
if (!vec) {
|
|
return -1;
|
|
}
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
int value = (int)i;
|
|
if (vector_push_back(vec, &value) != 0) {
|
|
vector_destroy(&vec);
|
|
return -1;
|
|
}
|
|
|
|
out[i] = vector_capacity(vec);
|
|
}
|
|
|
|
vector_destroy(&vec);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
@brief Reports the capacity of a freshly created vector in this translation unit.
|
|
@return The initial capacity, or 0 if creation failed.
|
|
*/
|
|
size_t vector_altcap_initial_capacity(void) {
|
|
vector_t* vec = vector_create(sizeof(int));
|
|
if (!vec) {
|
|
return 0;
|
|
}
|
|
|
|
size_t capacity = vector_capacity(vec);
|
|
vector_destroy(&vec);
|
|
return capacity;
|
|
}
|