mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-17 20:52:37 +01:00
TinySTL attempt 3. (#342)
* Update TinySTL (attempt 2). * Fix MSVC compilation issue for TinySTL. * Fix MSVC warning. * Favor ptrdiff_t for sizes in string. * Update genie.lua * Update math_test.cpp * Update test.h * Update test.h --------- Co-authored-by: Бранимир Караџић <branimirkaradzic@gmail.com>
This commit is contained in:
committed by
GitHub
parent
fae06fa431
commit
5fb8c15e15
@@ -20,6 +20,7 @@ TEST_CASE("isFinite, isInfinite, isNan", "[math]")
|
||||
for (uint64_t ii = 0; ii < UINT32_MAX; ii += rand()%(1<<13)+1)
|
||||
{
|
||||
union { uint32_t ui; float f; } u = { uint32_t(ii) };
|
||||
BX_UNUSED(u);
|
||||
|
||||
#if BX_PLATFORM_OSX || BX_PLATFORM_IOS
|
||||
REQUIRE(::__isnanf(u.f) == bx::isNan(u.f) );
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <bx/handlealloc.h>
|
||||
#include <bx/sort.h>
|
||||
#include <string>
|
||||
#include <tinystl/string.h>
|
||||
|
||||
bx::AllocatorI* g_allocator;
|
||||
|
||||
@@ -637,3 +638,93 @@ TEST_CASE("0terminated", "[string]")
|
||||
REQUIRE(2 == st.getLength() );
|
||||
REQUIRE(st.is0Terminated() );
|
||||
}
|
||||
|
||||
TEST(tinystl_string_constructor) {
|
||||
using tinystl::string;
|
||||
{
|
||||
string s;
|
||||
CHECK( s.size() == 0 );
|
||||
}
|
||||
{
|
||||
string s("hello");
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello") );
|
||||
}
|
||||
{
|
||||
string s("hello world", 5);
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello") );
|
||||
}
|
||||
{
|
||||
const string other("hello");
|
||||
string s = other;
|
||||
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello") );
|
||||
}
|
||||
{
|
||||
string other("hello");
|
||||
string s = std::move(other);
|
||||
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello") );
|
||||
CHECK( other.size() == 0 );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(tinystl_string_assign) {
|
||||
using tinystl::string;
|
||||
{
|
||||
const string other("hello");
|
||||
string s("new");
|
||||
s = other;
|
||||
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello") );
|
||||
}
|
||||
{
|
||||
string other("hello");
|
||||
string s("new");
|
||||
s = std::move(other);
|
||||
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello") );
|
||||
CHECK( other.size() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
const string other("hello longer string here");
|
||||
string s("short");
|
||||
s = other;
|
||||
|
||||
CHECK( s.size() == 24 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello longer string here") );
|
||||
}
|
||||
{
|
||||
string other("hello longer string here");
|
||||
string s("short");
|
||||
s = std::move(other);
|
||||
|
||||
CHECK( s.size() == 24 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "hello longer string here") );
|
||||
CHECK( other.size() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
const string other("short");
|
||||
string s("hello longer string here");
|
||||
s = other;
|
||||
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "short") );
|
||||
}
|
||||
{
|
||||
string other("short");
|
||||
string s("hello longer string here");
|
||||
s = std::move(other);
|
||||
|
||||
CHECK( s.size() == 5 );
|
||||
CHECK( 0 == strcmp(s.c_str(), "short") );
|
||||
CHECK( other.size() == 0 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
|
||||
BX_PRAGMA_DIAGNOSTIC_PUSH();
|
||||
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4312); // warning C4312 : 'reinterpret_cast' : conversion from 'int' to 'const char *' of greater size
|
||||
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wno-unknown-warning-option");
|
||||
BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wno-nan-infinity-disabled");
|
||||
#include <catch/catch_amalgamated.hpp>
|
||||
BX_PRAGMA_DIAGNOSTIC_POP();
|
||||
|
||||
|
||||
205
tests/unordered_map_test.cpp
Normal file
205
tests/unordered_map_test.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
/*-
|
||||
* Copyright 2012-2018 Matthew Endsley
|
||||
* All rights reserved
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted providing that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "test.h"
|
||||
|
||||
#include <tinystl/unordered_map.h>
|
||||
#include <tinystl/string.h>
|
||||
#include <utility>
|
||||
|
||||
template<typename K, typename V>
|
||||
static void comparemaps(const tinystl::unordered_map<K, V>& m, const tinystl::unordered_map<K, V>& expected) {
|
||||
CHECK( m.size() == expected.size() );
|
||||
|
||||
typedef typename tinystl::unordered_map<K, V>::const_iterator iterator;
|
||||
for (iterator it = expected.begin(), end = expected.end(); it != end; ++it) {
|
||||
iterator found = m.find((*it).first);
|
||||
CHECK( found != m.end() );
|
||||
CHECK( (*found).second == (*it).second );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uomap_constructor) {
|
||||
typedef tinystl::unordered_map<int, int> unordered_map;
|
||||
using tinystl::make_pair;
|
||||
|
||||
unordered_map baseline;
|
||||
comparemaps(baseline, baseline); // test with empty maps
|
||||
baseline.insert(make_pair(5, 1));
|
||||
baseline.insert(make_pair(6, 2));
|
||||
CHECK( 2 == baseline.size() );
|
||||
CHECK( baseline.find(5) != baseline.end() );
|
||||
CHECK( baseline[5] == 1 );
|
||||
CHECK( baseline.find(6) != baseline.end() );
|
||||
CHECK( baseline[6] == 2 );
|
||||
comparemaps(baseline, baseline);
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
|
||||
CHECK( m.empty() );
|
||||
CHECK( m.size() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map m = baseline;
|
||||
|
||||
comparemaps(m, baseline);
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map other = baseline;
|
||||
unordered_map m = std::move(other);
|
||||
|
||||
comparemaps(m, baseline);
|
||||
CHECK( other.empty() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uomap_assign) {
|
||||
typedef tinystl::unordered_map<int, int> unordered_map;
|
||||
using tinystl::make_pair;
|
||||
|
||||
unordered_map baseline;
|
||||
baseline.insert(make_pair(5, 1));
|
||||
baseline.insert(make_pair(6, 2));
|
||||
CHECK( 2 == baseline.size() );
|
||||
CHECK( baseline.find(5) != baseline.end() );
|
||||
CHECK( baseline[5] == 1 );
|
||||
CHECK( baseline.find(6) != baseline.end() );
|
||||
CHECK( baseline[6] == 2 );
|
||||
comparemaps(baseline, baseline);
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
m = baseline;
|
||||
|
||||
comparemaps(m, baseline);
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
for (int ii = 0; ii != 10; ++ii)
|
||||
m.insert(make_pair(ii, 10*ii));
|
||||
|
||||
m = baseline;
|
||||
|
||||
comparemaps(m, baseline);
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map other = baseline;
|
||||
unordered_map m;
|
||||
m = std::move(other);
|
||||
|
||||
comparemaps(m, baseline);
|
||||
CHECK( other.empty() );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map other = baseline;
|
||||
unordered_map m;
|
||||
for (int ii = 0; ii != 10; ++ii)
|
||||
m.insert(make_pair(ii, 10*ii));
|
||||
|
||||
m = std::move(other);
|
||||
|
||||
comparemaps(m, baseline);
|
||||
CHECK( other.empty() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uomap_insert) {
|
||||
using tinystl::string;
|
||||
using tinystl::pair;
|
||||
typedef tinystl::unordered_map<string, string> unordered_map;
|
||||
typedef pair<unordered_map::iterator, bool> inspair;
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
m.insert(make_pair(string("hello"), string("world")));
|
||||
CHECK( m.find("hello") != m.end() );
|
||||
}
|
||||
|
||||
{
|
||||
const pair<string, string> p("hello", "world");
|
||||
unordered_map m;
|
||||
inspair p1 = m.insert(p);
|
||||
CHECK( p1.second );
|
||||
CHECK( (*p1.first).first == tinystl::string("hello") );
|
||||
CHECK( (*p1.first).second == tinystl::string("world") );
|
||||
|
||||
inspair p2 = m.insert(p);
|
||||
CHECK( !p2.second );
|
||||
CHECK( p2.first == p1.first );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
m.emplace(pair<string, string>("hello", "world"));
|
||||
|
||||
CHECK( m.find("hello") != m.end() );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
inspair p1 = m.emplace(pair<string, string>("hello", "world"));
|
||||
CHECK( p1.second );
|
||||
CHECK( (*p1.first).first == tinystl::string("hello") );
|
||||
CHECK( (*p1.first).second == tinystl::string("world") );
|
||||
|
||||
inspair p2 = m.emplace(pair<string, string>("hello", "world"));
|
||||
CHECK( !p2.second );
|
||||
CHECK( p2.first == p1.first );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_map m;
|
||||
pair<string, string> p("hello", "world");
|
||||
m.emplace(std::move(p));
|
||||
|
||||
CHECK( m.find("hello") != m.end() );
|
||||
CHECK( p.first.size() == 0 );
|
||||
CHECK( p.second.size() == 0 );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uomap_iterate) {
|
||||
typedef tinystl::unordered_map<size_t, size_t> unordered_map;
|
||||
{
|
||||
unordered_map m;
|
||||
for (size_t i = 0; i < 1000; ++i) {
|
||||
CHECK( m.size() == i );
|
||||
size_t count = 0;
|
||||
for (auto it = m.begin(); it != m.end(); ++it) {
|
||||
count++;
|
||||
}
|
||||
CHECK( count == i );
|
||||
|
||||
m.insert(tinystl::make_pair(17 * i, 101 * i));
|
||||
}
|
||||
}
|
||||
}
|
||||
191
tests/unordered_set_test.cpp
Normal file
191
tests/unordered_set_test.cpp
Normal file
@@ -0,0 +1,191 @@
|
||||
/*-
|
||||
* Copyright 2012-2018 Matthew Endsley
|
||||
* All rights reserved
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted providing that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
||||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "test.h"
|
||||
|
||||
#include <tinystl/unordered_set.h>
|
||||
#include <tinystl/string.h>
|
||||
#include <utility>
|
||||
|
||||
template<typename T>
|
||||
static void comparesets(const tinystl::unordered_set<T>& s, const tinystl::unordered_set<T>& expected) {
|
||||
CHECK( s.size() == expected.size() );
|
||||
|
||||
typedef typename tinystl::unordered_set<T>::const_iterator iterator;
|
||||
for (iterator it = expected.begin(), end = expected.end(); it != end; ++it) {
|
||||
CHECK( s.find(*it) != s.end() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uoset_constructor) {
|
||||
typedef tinystl::unordered_set<int> unordered_set;
|
||||
|
||||
unordered_set baseline;
|
||||
comparesets(baseline, baseline); // test on empty
|
||||
baseline.insert(5);
|
||||
baseline.insert(6);
|
||||
CHECK( 2 == baseline.size() );
|
||||
CHECK( baseline.find(5) != baseline.end() );
|
||||
CHECK( baseline.find(6) != baseline.end() );
|
||||
comparesets(baseline, baseline);
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
|
||||
CHECK( s.empty() );
|
||||
CHECK( s.size() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set s = baseline;
|
||||
|
||||
comparesets(s, baseline);
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set other = baseline;
|
||||
unordered_set s = std::move(other);
|
||||
|
||||
comparesets(s, baseline);
|
||||
CHECK( other.empty() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uoset_assign) {
|
||||
typedef tinystl::unordered_set<int> unordered_set;
|
||||
|
||||
unordered_set baseline;
|
||||
baseline.insert(5);
|
||||
baseline.insert(6);
|
||||
CHECK( 2 == baseline.size() );
|
||||
CHECK( baseline.find(5) != baseline.end() );
|
||||
CHECK( baseline.find(6) != baseline.end() );
|
||||
comparesets(baseline, baseline);
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
s = baseline;
|
||||
|
||||
comparesets(s, baseline);
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
for (int ii = 0; ii != 10; ++ii)
|
||||
s.insert(ii);
|
||||
|
||||
s = baseline;
|
||||
|
||||
comparesets(s, baseline);
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set other = baseline;
|
||||
unordered_set s;
|
||||
s = std::move(other);
|
||||
|
||||
comparesets(s, baseline);
|
||||
CHECK( other.empty() );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set other = baseline;
|
||||
unordered_set s;
|
||||
for (int ii = 0; ii != 10; ++ii)
|
||||
s.insert(ii);
|
||||
|
||||
s = std::move(other);
|
||||
|
||||
comparesets(s, baseline);
|
||||
CHECK( other.empty() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uoset_insert) {
|
||||
typedef tinystl::unordered_set<tinystl::string> unordered_set;
|
||||
typedef tinystl::pair<unordered_set::iterator, bool> pair;
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
s.insert("hello");
|
||||
CHECK( s.find("hello") != s.end() );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
pair p1 = s.insert("hello");
|
||||
CHECK( p1.second );
|
||||
CHECK( (*p1.first) == tinystl::string("hello") );
|
||||
|
||||
pair p2 = s.insert("hello");
|
||||
CHECK( !p2.second );
|
||||
CHECK( p2.first == p1.first );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
s.emplace("hello");
|
||||
|
||||
CHECK( s.find("hello") != s.end() );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
pair p1 = s.emplace("hello");
|
||||
CHECK( p1.second );
|
||||
CHECK( (*p1.first) == tinystl::string("hello") );
|
||||
|
||||
pair p2 = s.emplace("hello");
|
||||
CHECK( !p2.second );
|
||||
CHECK( p2.first == p1.first );
|
||||
}
|
||||
|
||||
{
|
||||
unordered_set s;
|
||||
tinystl::string key("hello");
|
||||
s.emplace(std::move(key));
|
||||
|
||||
CHECK( s.find("hello") != s.end() );
|
||||
CHECK( key.size() == 0 );
|
||||
}
|
||||
}
|
||||
|
||||
TEST(uoset_iterate) {
|
||||
typedef tinystl::unordered_set<int> unordered_set;
|
||||
{
|
||||
unordered_set s;
|
||||
for (size_t i = 0; i < 1000; ++i) {
|
||||
CHECK( s.size() == i );
|
||||
size_t count = 0;
|
||||
for (auto it = s.begin(); it != s.end(); ++it) {
|
||||
count++;
|
||||
}
|
||||
CHECK( count == i );
|
||||
|
||||
s.insert(int(17 * i));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user