feat: imrpovement and add more methods to Option and Result

This commit is contained in:
DL
2024-02-28 15:16:01 +01:00
parent 8a96704909
commit 47a2d0a64d
2 changed files with 513 additions and 54 deletions

View File

@ -2,12 +2,19 @@
#include "../src/ro.h"
#include <functional>
#include <iostream>
#include <cstring>
using namespace std;
using namespace ro;
struct Stat{
int passed;
int total;
};
static Stat g_stat = {0,0};
#define assert(v,fmt,...) if(!(v)) { \
throw Error(ro::sfmt("ASSERT ERROR %s:%d: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)); \
}
@ -19,12 +26,14 @@ void test(const char* desc, const std::function<void()>& lambda)
try {
lambda();
std::cout << "OK" << std::endl;
g_stat.passed++;
}
catch(ro::Error e)
{
std::cout << "FAILED" << std::endl;
std::cout << e.what() << std::endl;
}
g_stat.total++;
}
int main(int argc, char const *argv[])
@ -52,6 +61,14 @@ int main(int argc, char const *argv[])
assert(ret.unwrap() == "abc", "Data mistmatch");
});
test("OPtion get_or_insert", [](){
auto x = None(int);
int y = x.get_or_insert(5);
assert(y == 5, "Unexpected value");
assert(x == 5, "Unexpected value");
});
test("Option map", [](){
auto fn = [](int x){
return "Hello";
@ -67,7 +84,7 @@ int main(int argc, char const *argv[])
});
test("Option map or", [](){
test("Option map_or", [](){
auto fn = [](int x){
return ++x;
};
@ -81,6 +98,140 @@ int main(int argc, char const *argv[])
assert(v == 3, "Value mismatch %d",v);
});
test("Option filter", [](){
auto fn = [](int n) {
return n % 2 == 0;
};
//Option<int> opt = None(int).filter(fn);
assert(None(int).filter(fn).is_none(), "Filter value shall be None");
assert(Some(3).filter(fn).is_none(), "Filter value shall be None");
assert(Some(4).filter(fn).is_some(), "Filter value shall not be None");
});
test("Option OR", [](){
auto x = Some(2);
auto y = None(int);
assert((x | y) == Some(2), "Unexpected value");
x = None(int);
y = Some(100);
assert((x | y) == Some(100), "Unexpected value");
x = Some(2);
y = Some(100);
assert((x|y) == Some(2), "Unexpected value");
x = None(int);
y = None(int);
assert((x | y) == None(int), "Unexpected value");
});
test("Option AND", [](){
auto x = Some(2);
auto y = None(int);
assert((x & y) == None(int), "Unexpected value");
x = None(int);
y = Some(100);
assert((x & y) == None(int), "Unexpected value");
x = Some(2);
y = Some(100);
assert((x&y) == Some(100), "Unexpected value");
x = None(int);
y = None(int);
assert((x & y) == None(int), "Unexpected value");
});
test("Option XOR", [](){
auto x = Some(2);
auto y = None(int);
assert((x ^ y) == Some(2), "Unexpected value");
x = None(int);
y = Some(100);
assert((x ^ y) == Some(100), "Unexpected value");
x = Some(2);
y = Some(100);
assert((x^y) == None(int), "Unexpected value");
x = None(int);
y = None(int);
assert((x ^ y) == None(int), "Unexpected value");
});
test("Option or_else", [](){
auto nobody = [](){return None(int);};
auto vikings = [](){return Some(100);};
assert(None(int).or_else(vikings) == Some(100), "Unexpected value");
assert(None(int).or_else(nobody) == None(int), "Unexpected value");
assert(Some(2).or_else(vikings) == Some(2), "Unexpected value");
});
test("Option and_then", [](){
auto fn = [](int x){return string("Hello");};
assert(Some(1).and_then<string>(fn) == Some(string("Hello")), "Unexpected value");
assert(None(int).and_then<string>(fn) == None(string), "Unexpected value");
});
test("Option take", [](){
auto x = Some(2);
auto y = x.take();
assert(x == None(int), "Unexpected value");
assert(y == Some(2), "Unexpected value");
auto z = None(int);
y = z.take();
assert(x == None(int), "Unexpected value");
assert(y == None(int), "Unexpected value");
});
test("Option take_if", [](){
auto fn = [](int n) {
return n % 2 == 0;
};
auto x = Some(2);
auto y = x.take_if(fn);
assert(x == None(int), "Unexpected value");
assert(y == Some(2), "Unexpected value");
auto z = Some(3);
y = z.take_if(fn);
assert(z == Some(3), "Unexpected value");
assert(y == None(int), "Unexpected value");
});
test("Option unwrap_or", [](){
assert(Some(2).unwrap_or(4) == 2, "Unexpected value");
assert(None(int).unwrap_or(4) == 4, "Unexpected value");
assert(None(Error).unwrap_or(Error("Hello")) == Error("Hello"), "Unexpected value");
});
test("Option unwrap_or_else", [](){
assert(Some(2).unwrap_or_else([](){ return 4; }) == 2, "Unexpected value");
assert(None(int).unwrap_or_else([](){ return 4; }) == 4, "Unexpected value");
});
test("Option zip", [](){
auto x = Some(2);
auto y = Some(string("Hello"));
auto z = None(int);
assert(x.zip(y).unwrap() == std::tuple(2, string("Hello")), "Unexpected value");
assert(x.zip(z).is_none(), "Unexpected value");
});
test("Test Result", []{
Result<string, Error> ret = ERR("Error");
assert(ret.is_err(), "Object should containe error object");
@ -132,7 +283,7 @@ int main(int argc, char const *argv[])
});
test("Result map err", [](){
test("Result map_err", [](){
auto fn = [](int x){
return "Hello";
};
@ -145,7 +296,7 @@ int main(int argc, char const *argv[])
assert(ret1.err().unwrap() == "Hello", "Value mistmatch");
});
test("Result map or", [](){
test("Result map_or", [](){
auto fn = [](int x){
return ++x;
};
@ -159,4 +310,91 @@ int main(int argc, char const *argv[])
assert(v == 3, "Value mismatch %d",v);
});
test("Result AND", [](){
Result<int, Error> x = 2;
Result<int, Error> y = Error("Hello");
assert((x&y) == Error("Hello"), "Value mismatch");
x = Error("Error1");
y = 10;
assert((x&y) == Error("Error1"), "Value mismatch");
x = Error("Error1");
y = Error("Error2");
assert((x&y) == Error("Error1"), "Value mismatch");
x = 1;
y = 10;
assert((x&y) == 10, "Value mismatch");
});
test("Result OR", [](){
Result<int, Error> x = 2;
Result<int, Error> y = Error("Hello");
assert((x|y) == 2, "Value mismatch");
x = Error("Error1");
y = 10;
assert((x|y) == 10, "Value mismatch");
x = Error("Error1");
y = Error("Error2");
assert((x|y) == Error("Error2"), "Value mismatch");
x = 1;
y = 10;
assert((x|y) == 1, "Value mismatch");
});
test("Result and_then", [](){
auto fn = [](int x){return string("Hello");};
Result<int, Error> x = 2;
auto test = Result<string,Error>(string("Hello"));
assert(x.and_then<string>(fn) == string("Hello"), "Unexpected value");
x = Error("Error");
assert(x.and_then<string>(fn) == Error("Error"), "Unexpected value");
});
test("Result or_else", [](){
auto sq = [](int x){return x*x;};
auto err = [](int x){return x;};
Result<double, int> x(2.0);
auto ret = x.or_else<int>(sq).or_else<int>(sq);
assert(ret == 2.0, "Unexpected value");
ret = x.or_else<int>(err).or_else<int>(sq);
assert(ret == 2.0, "Unexpected value");
x = 3;
ret = x.or_else<int>(sq).or_else<int>(err);
assert(ret == 9, "Unexpected value");
ret = x.or_else<int>(err).or_else<int>(err);
assert(ret == 3, "Unexpected value");
});
test("Result unwrap_err", [](){
Result<int, Error> x = Error("Hello");
assert(x.unwrap_err() == Error("Hello"), "Unexpected value");
});
test("Result unwrap_or", [](){
Result<int, Error> x = 2;
assert(x.unwrap_or(10) == 2, "Unexpected value");
x = ERR("Error");
assert(x.unwrap_or(10) == 10, "Unexpected value");
});
test("Result unwrap_or_else", [](){
auto fn = [](const Error& x){return strlen(x.what());};
Result<int, Error> x = 2;
assert(x.unwrap_or_else(fn) == 2, "Unexpected value");
x = Error("HELLO");
assert(x.unwrap_or_else(fn) == 5, "Unexpected value");
});
cout << "REPORT: " << g_stat.passed << " tests passed / " << g_stat.total << " tests." << endl;
}