nngn
Loading...
Searching...
No Matches
tests.h
Go to the documentation of this file.
1#ifndef NNGN_TESTS_TESTS_H
2#define NNGN_TESTS_TESTS_H
3
4#include <sstream>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include <QTest>
10
11#include "debug.h"
12
13#include "graphics/graphics.h"
14#include "utils/literals.h"
15
16using namespace nngn::literals;
17using nngn::u32;
18
19namespace std {
20
21inline char *toString(const string &s) { return QTest::toString(s.c_str()); }
22
23inline char *toString(string_view s) {
24 return QTest::toString(
25 QString::fromUtf8(s.data(), static_cast<int>(s.size())));
26}
27
28template<typename T, typename A>
29requires(requires(ostream &o, T t) { o << t; })
30char *toString(const vector<T, A> &v) {
31 stringstream s;
32 s << v;
33 return toString(s.str());
34}
35
36}
37
38inline constexpr auto fuzzy_eq_e = 1_u32 << 4;
39
40inline bool fuzzy_eq(float f0, float f1, u32 e = fuzzy_eq_e) {
41 if(f0 == 0 || f1 == 0)
42 ++f0, ++f1;
43 return qFloatDistance(f0, f1) < e;
44}
45
46template<template<typename> typename V, typename T, std::size_t N>
47inline bool fuzzy_eq(
48 const nngn::vec<V, T, N> &v0,
49 const nngn::vec<V, T, N> &v1,
50 u32 e = fuzzy_eq_e)
51{
52 for(std::size_t i = 0; i < N; ++i)
53 if(!fuzzy_eq(v0[i], v1[i], e))
54 return false;
55 return true;
56}
57
58template<typename T>
59inline bool fuzzy_eq(
60 const nngn::mat4_base<T> &m0,
61 const nngn::mat4_base<T> &m1,
62 u32 e = fuzzy_eq_e)
63{
64 for(std::size_t i = 0; i != 4; ++i)
65 if(!fuzzy_eq(m0[i], m1[i], e))
66 return false;
67 return true;
68}
69
70namespace nngn {
71
72namespace {
73
74template<typename T>
75inline char *to_string(const T &v) {
76 std::stringstream s;
77 s << v;
78 return QTest::toString(s.str().c_str());
79}
80
81}
82
83#define F(T) inline char *toString(const T &t) { return to_string(t); }
84template<typename T> F(vec2_base<T>)
85template<typename T> F(vec3_base<T>)
86template<typename T> F(vec4_base<T>)
90#undef F
91
92}
93
94#endif
for i
Definition font.lua:5
#define T(f0, f1, f2)
local N
Definition gamma.lua:5
char * to_string(const T &v)
Definition tests.h:75
Definition literals.h:9
Definition audio.cpp:7
std::uint32_t u32
Definition def.h:14
Definition debug.h:13
char * toString(const string &s)
Definition tests.h:21
#define F(...)
Definition pp.cpp:12
v[1]
Definition math.lua:22
Definition graphics.h:136
Definition mat3.h:12
Definition mat4.h:12
Definition vec2.h:11
Definition vec3.h:12
Definition vec4.h:12
Definition vec.h:22
local e
Definition math.lua:4
bool fuzzy_eq(float f0, float f1, u32 e=fuzzy_eq_e)
Definition tests.h:40
constexpr auto fuzzy_eq_e
Definition tests.h:38
std::chrono::seconds s
Definition timing.cpp:6