testing builtins: uniq, unique, typeof

This commit is contained in:
Robert Bendun 2022-09-25 12:01:19 +02:00
parent 225befb22b
commit 68f5298e39
5 changed files with 27 additions and 9 deletions

View File

@ -1,12 +1,12 @@
MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)" MAKEFLAGS="-j $(grep -c ^processor /proc/cpuinfo)"
CXXFLAGS:=$(CXXFLAGS) -std=c++20 -Wall -Wextra -Werror=switch -Werror=return-type -Werror=unused-result -Wno-maybe-uninitialized CXXFLAGS:=$(CXXFLAGS) -std=c++20 -Wall -Wextra -Werror=switch -Werror=return-type -Werror=unused-result -Wno-maybe-uninitialized
CPPFLAGS:=$(CPPFLAGS) -Ilib/expected/ -Ilib/ut/ -Ilib/midi/include -I. -Ilib/bestline/ CPPFLAGS:=$(CPPFLAGS) -Ilib/expected/ -Ilib/midi/include -I. -Ilib/bestline/
RELEASE_FLAGS=-O3 RELEASE_FLAGS=-O2
DEBUG_FLAGS=-O0 -ggdb -fsanitize=undefined -DDebug DEBUG_FLAGS=-O0 -ggdb -fsanitize=undefined -DDebug
CXX=g++ CXX=g++
LDFLAGS=-L./lib/midi/ LDFLAGS=-L./lib/midi/ -flto
LDLIBS=-lmidi-alsa -lasound -lpthread -static-libgcc -static-libstdc++ LDLIBS=-lmidi-alsa -lasound -lpthread -static-libgcc -static-libstdc++

View File

@ -21,13 +21,13 @@ void Interpreter::register_callbacks()
/// Check if type has index method /// Check if type has index method
template<typename T> template<typename T>
concept With_Index_Method = requires (T t, Interpreter interpreter, usize position) { concept With_Index_Method = requires (T &t, Interpreter interpreter, usize position) {
{ t.index(interpreter, position) } -> std::convertible_to<Result<Value>>; { t.index(interpreter, position) } -> std::convertible_to<Result<Value>>;
}; };
/// Check if type has either (index operator or method) and size() method /// Check if type has either (index operator or method) and size() method
template<typename T> template<typename T>
concept Iterable = (With_Index_Method<T> || With_Index_Operator<T>) && requires (T const t) { concept Iterable = (With_Index_Method<T> || With_Index_Operator<T>) && requires (T const& t) {
{ t.size() } -> std::convertible_to<usize>; { t.size() } -> std::convertible_to<usize>;
}; };
@ -44,7 +44,8 @@ static Result<std::vector<Value>> deep_flat(Interpreter &interpreter, Iterable a
} }
if (auto collection = get_if<Collection>(element)) { if (auto collection = get_if<Collection>(element)) {
std::ranges::move(Try(deep_flat(interpreter, *collection)), std::back_inserter(result)); auto array = Try(deep_flat(interpreter, *collection));
std::move(array.begin(), array.end(), std::back_inserter(result));
} else { } else {
result.push_back(std::move(element)); result.push_back(std::move(element));
} }
@ -709,7 +710,7 @@ static Result<Value> builtin_reverse(Interpreter &i, std::vector<Value> args)
/// Get minimum of arguments /// Get minimum of arguments
static Result<Value> builtin_min(Interpreter &i, std::vector<Value> args) static Result<Value> builtin_min(Interpreter &i, std::vector<Value> args)
{ {
auto array = Try(deep_flat(i, std::move(args))); auto array = Try(deep_flat(i, args));
if (auto min = std::ranges::min_element(array); min != array.end()) if (auto min = std::ranges::min_element(array); min != array.end())
return *min; return *min;
return Value{}; return Value{};
@ -718,7 +719,7 @@ static Result<Value> builtin_min(Interpreter &i, std::vector<Value> args)
/// Get maximum of arguments /// Get maximum of arguments
static Result<Value> builtin_max(Interpreter &i, std::vector<Value> args) static Result<Value> builtin_max(Interpreter &i, std::vector<Value> args)
{ {
auto array = Try(deep_flat(i, std::move(args))); auto array = Try(deep_flat(i, args));
if (auto max = std::ranges::max_element(array); max != array.end()) if (auto max = std::ranges::max_element(array); max != array.end())
return *max; return *max;
return Value{}; return Value{};
@ -741,7 +742,7 @@ static Result<Value> builtin_partition(Interpreter &i, std::vector<Value> args)
Array tuple[2] = {}; Array tuple[2] = {};
for (auto &value : array) { for (auto &value : array) {
tuple[Try(predicate(i, { std::move(value) })).truthy()].elements.push_back(std::move(value)); tuple[Try(predicate(i, { value })).truthy()].elements.push_back(std::move(value));
} }
return Array {{ return Array {{

View File

@ -0,0 +1,7 @@
say (typeof (call flat));
say (typeof 0);
say (typeof []);
say (typeof c);
say (typeof false);
say (typeof nil);
say (typeof say);

View File

@ -0,0 +1,5 @@
say (uniq (up 10 & down 10));
say (uniq [1;1;1;3;5;3;4;4;1]);
-- Multiple uniq applications shouldn't matter
say (uniq (uniq [1;1;1;3;5;3;4;4;1]));

View File

@ -0,0 +1,5 @@
say (unique (up 10 & down 10));
say (unique [1;1;1;3;5;3;4;4;1]);
-- Multiple unique applications shouldn't matter
say (unique (unique [1;1;1;3;5;3;4;4;1]));