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 138 139 140 141 142 143 144 145 146
|
// Licensed under the MIT License.
// Copyright David LeBlanc - dcl@dleblanc.net
#pragma once
#include <iostream>
#include <ios>
#include <iomanip>
#include <sstream>
enum OpType
{
Mult,
Div,
Div2,
Add,
Sub
};
struct OpName
{
static const char* op_name(int op)
{
switch (op)
{
case OpType::Mult:
return "Mult";
case OpType::Div:
case OpType::Div2:
return "Div";
case OpType::Add:
return "Add";
case OpType::Sub:
return "Sub";
default:
return "";
}
}
};
template <typename T, typename U, int op>
struct TestCase
{
TestCase(T _x, U _y, bool f) :
x(_x), y(_y), fExpected(f) { }
T x;
U y;
bool fExpected;
};
template <typename T, typename U, int op>
class TestVector
{
public:
TestVector() : test_cases(nullptr), count(0), current(0) {}
void Reset() { current = 0; }
bool Done() const { return current >= count; }
TestCase<T, U, op> GetNext()
{
const TestCase<T, U, op>* next = GetCase(current);
current++;
return *next;
}
private:
const TestCase<T, U, op>* GetCase(size_t i)
{
if (i > count - 1)
return nullptr;
return &test_cases[i];
}
const TestCase<T, U, op>* test_cases;
size_t count;
size_t current;
};
#if !defined(COUNTOF)
# if defined(_countof)
# define COUNTOF(x) _countof(x)
# else
# define COUNTOF(x) (sizeof(x)/sizeof((x)[0]))
# endif
#endif
template <typename T>
std::string to_hex(T t)
{
std::ostringstream ostm;
ostm << "0x" << std::setfill('0') << std::hex << std::setw(sizeof(t) <= 4 ? 8 : 16) << t;
return ostm.str();
}
template <>
inline std::string to_hex< uint8_t >(uint8_t t)
{
std::ostringstream ostm;
ostm << "0x" << std::setfill('0') << std::hex << std::setw(2) << static_cast<uint16_t>(t);
return ostm.str();
}
template <>
inline std::string to_hex< int8_t >(int8_t t)
{
std::ostringstream ostm;
ostm << "0x" << std::setfill('0') << std::hex << std::setw(2) << static_cast<uint16_t>(t);
return ostm.str();
}
template <typename T, typename U>
void err_msg(const std::string& msg, T t, U u, bool expected)
{
std::cerr << msg << to_hex(t) << ", " << to_hex(u) << ", expected = " << expected << std::endl;
}
template <typename T>
void err_msg(const std::string& msg, T t, bool expected)
{
std::cerr << msg << to_hex(t) << ", expected = " << expected << std::endl;
}
class trace
{
public:
trace(bool f = false) : enable(f){}
void trace_msg(const std::string& msg)
{
if(enable)
std::cout << msg << std::endl;
}
void trace_msg(const char* msg)
{
if(enable)
trace_msg(std::string(msg));
}
bool enable;
};
extern "C" void add_test();
|