mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[kutil] Add sorted insert to kutil::vector
Added a sorted insert to vector, as well as cleaning up and extending the removal functions.
This commit is contained in:
13
src/tests/container_helpers.h
Normal file
13
src/tests/container_helpers.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
struct unsortableT {
|
||||
int value;
|
||||
};
|
||||
|
||||
struct sortableT {
|
||||
int value;
|
||||
int compare(const sortableT *other) const {
|
||||
return value - other->value;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,22 +5,12 @@
|
||||
#include <vector>
|
||||
#include "kutil/linked_list.h"
|
||||
#include "catch.hpp"
|
||||
#include "container_helpers.h"
|
||||
|
||||
using namespace kutil;
|
||||
|
||||
const int test_list_size = 100;
|
||||
|
||||
struct unsortableT {
|
||||
int value;
|
||||
};
|
||||
|
||||
struct sortableT {
|
||||
int value;
|
||||
int compare(const sortableT *other) {
|
||||
return value - other->value;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class ListVectorCompare :
|
||||
public Catch::MatcherBase<std::vector<list_node<T>>>
|
||||
|
||||
25
src/tests/vector.cpp
Normal file
25
src/tests/vector.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <vector>
|
||||
#include "kutil/vector.h"
|
||||
#include "catch.hpp"
|
||||
#include "container_helpers.h"
|
||||
|
||||
TEST_CASE( "sorted vector tests", "[containers] [vector]" )
|
||||
{
|
||||
using clock = std::chrono::system_clock;
|
||||
unsigned seed = clock::now().time_since_epoch().count();
|
||||
std::default_random_engine rng(seed);
|
||||
std::uniform_int_distribution<int> distrib(0,10000);
|
||||
|
||||
kutil::vector<sortableT> v;
|
||||
|
||||
int sizes[] = {1, 2, 3, 5, 100};
|
||||
for (int s : sizes) {
|
||||
for (int i = 0; i < s; ++i) {
|
||||
sortableT t { distrib(rng) };
|
||||
v.sorted_insert(t);
|
||||
}
|
||||
|
||||
for (int i = 1; i < s; ++i)
|
||||
CHECK( v[i].value >= v[i-1].value );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user