tests
This commit is contained in:
parent
b872cc940b
commit
734628e604
134
src/main.cc
134
src/main.cc
|
@ -4,7 +4,6 @@
|
|||
#include "ranges/enumerate.hpp"
|
||||
#include "ranges/first.hpp"
|
||||
#include "ranges/generated.hpp"
|
||||
#include "tagged_union.hpp"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
@ -19,56 +18,101 @@
|
|||
#include <vector>
|
||||
|
||||
using namespace std::string_view_literals;
|
||||
using namespace util::bitflag_operators;
|
||||
|
||||
enum class E {
|
||||
A = 0x1,
|
||||
B = 0x10,
|
||||
C = 0x100,
|
||||
D = 0x1000,
|
||||
};
|
||||
#include "iterator.hpp"
|
||||
|
||||
auto test_iterator_t() {
|
||||
int *ptr = nullptr;
|
||||
int arr[5] = {0, 1, 2, 3, 4};
|
||||
using int_ptr_iterator_t = util::iterator_t<int *>;
|
||||
auto a = int_ptr_iterator_t(arr);
|
||||
|
||||
const auto three = a[3];
|
||||
a += 2;
|
||||
*a = 6;
|
||||
|
||||
for (auto &&e : arr) {
|
||||
util::println("{}", e);
|
||||
}
|
||||
}
|
||||
|
||||
auto test_enumerate_view() {
|
||||
auto enumerated = util::views::from([] { return 1; }) |
|
||||
util::views::enumerate | std::views::take(10);
|
||||
|
||||
auto b = std::ranges::begin(enumerated);
|
||||
|
||||
for (auto &&[i, e] : enumerated) {
|
||||
util::println("[{}] {}", i, e);
|
||||
}
|
||||
|
||||
for (auto &&[i, e] : enumerated | util::views::enumerate) {
|
||||
util::println("[{}] {}", i, e.value);
|
||||
}
|
||||
|
||||
auto transformed_enumerate =
|
||||
util::views::from([] { return 1; }) |
|
||||
std::views::transform([](auto &&i) { return i + 1; }) |
|
||||
util::views::enumerate | std::views::take(10);
|
||||
|
||||
for (auto &&[i, e] : transformed_enumerate) {
|
||||
util::println("[{}] {}", i, e);
|
||||
}
|
||||
}
|
||||
|
||||
auto test_generated_view() {
|
||||
const auto random =
|
||||
util::views::from([] {
|
||||
return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"
|
||||
[std::rand() %
|
||||
std::strlen(
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY")];
|
||||
}) |
|
||||
std::views::take(10) | util::views::collect<std::string>;
|
||||
|
||||
util::println("random string: {}", random);
|
||||
}
|
||||
|
||||
auto test_collect() {
|
||||
auto vec = std::vector<int>({1, 2, 3, 4, 5});
|
||||
auto range = vec | std::views::filter([](auto &i) { return i % 2; });
|
||||
|
||||
const auto set = util::collect<std::set<int>>(range);
|
||||
}
|
||||
|
||||
auto test_first() {
|
||||
const auto random =
|
||||
util::views::from([] {
|
||||
return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"
|
||||
[std::rand() %
|
||||
std::strlen(
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY")];
|
||||
}) |
|
||||
std::views::take(10) | util::views::collect<std::string>;
|
||||
|
||||
const auto first = random | util::views::first;
|
||||
util::println("{}", first);
|
||||
}
|
||||
|
||||
auto test_assert_no_fail() {
|
||||
util::assert_ne("asdf"sv, "nsdf"sv);
|
||||
util::assert_eq(1, 1);
|
||||
util::assert_eq(1 == 1, true);
|
||||
util::assert_eq(0, false);
|
||||
}
|
||||
|
||||
auto test_panic() { util::panic("asdf {}", 42); }
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
printf("Hello, Alloy!\n");
|
||||
|
||||
const auto a = E::A;
|
||||
const auto ab = E::A | E::B;
|
||||
const auto all = E::A | E::B | E::C | E::D;
|
||||
|
||||
auto vec = std::vector<int>({1, 2, 3, 4, 5});
|
||||
|
||||
auto range = vec | std::views::filter([](auto &i) { return i % 2; });
|
||||
auto infinite_range = util::views::from([] {return 1;}) | std::views::take(5);
|
||||
|
||||
auto map =
|
||||
vec | util::views::enumerate | util::views::collect<std::map<int, int>>;
|
||||
|
||||
for (auto &&[i, e] : map) {
|
||||
util::println("[{}] {}", i, e);
|
||||
}
|
||||
|
||||
auto random = util::views::from([] {
|
||||
return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"
|
||||
[std::rand() %
|
||||
std::strlen(
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY")];
|
||||
}) |
|
||||
std::views::take(10) | util::views::collect<std::string>;
|
||||
|
||||
auto first = random | util::views::first;
|
||||
|
||||
util::println("{}", first);
|
||||
util::println("{}", random);
|
||||
|
||||
for (auto&& i : infinite_range) {
|
||||
util::print("{}\n", i);
|
||||
}
|
||||
|
||||
const auto set = util::collect<std::set<int>>(range);
|
||||
test_iterator_t();
|
||||
test_enumerate_view();
|
||||
test_collect();
|
||||
test_generated_view();
|
||||
test_first();
|
||||
test_assert_no_fail();
|
||||
|
||||
util::print("hello {}\n", "world");
|
||||
|
||||
util::assert_ne("asdf"sv, "nsdf"sv);
|
||||
util::panic("asdf {}", 42);
|
||||
return 1;
|
||||
}
|
Loading…
Reference in a new issue