Skip to content

Instantly share code, notes, and snippets.

@adriweb
Created August 1, 2018 00:42
Show Gist options
  • Save adriweb/1eb36aa2c7331997723602e14f13288b to your computer and use it in GitHub Desktop.
Save adriweb/1eb36aa2c7331997723602e14f13288b to your computer and use it in GitHub Desktop.
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <tice.h>
#include <graphx.h>
#ifdef __ZILOG__
extern int testtt(void);
int main() { return testtt(); }
#endif
#include <TINYSTL/unordered_map.h>
#include <TINYSTL/string.h>
namespace std
{
template< class T > struct remove_reference {typedef T type;};
template< class T > struct remove_reference<T&> {typedef T type;};
template< class T > struct remove_reference<T&&> {typedef T type;};
template <typename T>
typename remove_reference<T>::type&& move(T&& arg)
{
return static_cast<typename remove_reference<T>::type&&>(arg);
}
}
void* operator new(size_t sz) { return ::malloc(sz); }
void operator delete(void* ptr) noexcept { ::free(ptr); }
void printText(const char *text, uint8_t x, uint8_t y);
#define CHECK(cond) do { if (!(cond)) { printText("Failed!", 0, 0); while (!os_GetCSC()); return 1; } } while(0)
using namespace tinystl;
template<typename K, typename V>
static int 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 );
}
}
int main(void)
{
{
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;
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() );
}
}
{
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() );
}
}
{
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 );
}
}
printText("All good!", 0, 0);
/*
long test42 = 42123456;
char buf[20] = {0};
sprintf(buf, "[ %ld ]", test42);
printText(buf, 0, 2);
*/
while (!os_GetCSC()); return 0;
}
void printText(const char *text, uint8_t xpos, uint8_t ypos)
{
os_SetCursorPos(ypos, xpos);
os_PutStrFull(text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment