aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/list/cons.h23
-rw-r--r--src/list/list.h26
-rw-r--r--test.cc24
3 files changed, 69 insertions, 4 deletions
diff --git a/src/list/cons.h b/src/list/cons.h
new file mode 100644
index 0000000..b858c4c
--- /dev/null
+++ b/src/list/cons.h
@@ -0,0 +1,23 @@
+#ifndef TYPEASVALUE_SRC_LIST_CONS_H_
+#define TYPEASVALUE_SRC_LIST_CONS_H_
+
+namespace tav {
+
+template <
+ typename CAR,
+ typename CDR
+>
+struct Cons {
+ typedef CAR car;
+ typedef CDR cdr;
+};
+
+template <typename Cons>
+using Car = typename Cons::car;
+
+template <typename Cons>
+using Cdr = typename Cons::cdr;
+
+}
+
+#endif // TYPEASVALUE_SRC_LIST_CONS_H_
diff --git a/src/list/list.h b/src/list/list.h
new file mode 100644
index 0000000..47d0d2f
--- /dev/null
+++ b/src/list/list.h
@@ -0,0 +1,26 @@
+#ifndef TYPEASVALUE_SRC_LIST_LIST_H_
+#define TYPEASVALUE_SRC_LIST_LIST_H_
+
+#include "cons.h"
+
+namespace tav {
+
+template <
+ typename Head,
+ typename... Tail
+>
+struct List {
+ typedef Cons<
+ Head,
+ typename List<Tail...>::type
+ > type;
+};
+
+template <typename Head>
+struct List<Head> {
+ typedef Cons<Head, void> type;
+};
+
+}
+
+#endif // TYPEASVALUE_SRC_LIST_LIST_H_
diff --git a/test.cc b/test.cc
index 4e97b7a..e2b7186 100644
--- a/test.cc
+++ b/test.cc
@@ -3,6 +3,8 @@
#include "type.h"
#include "operation/math.h"
#include "conditional/if.h"
+#include "list/cons.h"
+#include "list/list.h"
class TypeAsValueTest : public ::testing::Test { };
@@ -11,10 +13,10 @@ TEST_F(TypeAsValueTest, Value) {
}
TEST_F(TypeAsValueTest, BasicMath) {
- EXPECT_EQ(3, ( tav::add<tav::Int<1>, tav::Int<2>>::value ));
- EXPECT_EQ(4, ( tav::substract<tav::Int<10>, tav::Int<6>>::value ));
- EXPECT_EQ(42, ( tav::multiply<tav::Int<2>, tav::Int<21>>::value ));
- EXPECT_EQ(5, ( tav::divide<tav::Int<10>, tav::Int<2>>::value ));
+ EXPECT_EQ(3, ( tav::Add<tav::Int<1>, tav::Int<2>>::value ));
+ EXPECT_EQ(4, ( tav::Substract<tav::Int<10>, tav::Int<6>>::value ));
+ EXPECT_EQ(42, ( tav::Multiply<tav::Int<2>, tav::Int<21>>::value ));
+ EXPECT_EQ(5, ( tav::Divide<tav::Int<10>, tav::Int<2>>::value ));
}
TEST_F(TypeAsValueTest, Conditional) {
@@ -22,6 +24,20 @@ TEST_F(TypeAsValueTest, Conditional) {
EXPECT_EQ(2, ( tav::If<false, tav::Int<1>, tav::Int<2>>::value ));
}
+TEST_F(TypeAsValueTest, Cons) {
+ EXPECT_EQ(1, ( tav::Car<tav::Cons<tav::Int<1>, void>>::value ));
+ EXPECT_EQ(1, ( tav::Car<tav::Cons<tav::Int<1>, tav::Int<2>>>::value ));
+ EXPECT_EQ(2, ( tav::Cdr<tav::Cons<tav::Int<1>, tav::Int<2>>>::value ));
+ EXPECT_EQ(2, ( tav::Car<tav::Cdr<tav::Cons<tav::Int<1>, tav::Cons<tav::Int<2>, tav::Int<3>>>>>::value ));
+}
+
+TEST_F(TypeAsValueTest, List) {
+ EXPECT_EQ(1, ( tav::Car<tav::List<tav::Int<1>>::type>::value ));
+ EXPECT_EQ(1, ( tav::Car<tav::List<tav::Int<1>, tav::Int<2>>::type>::value ));
+ EXPECT_EQ(2, ( tav::Car<tav::Cdr<tav::List<tav::Int<1>, tav::Int<2>>::type>>::value ));
+ EXPECT_EQ(2, ( tav::Car<tav::Cdr<tav::List<tav::Int<1>, tav::Int<2>, tav::Int<3>>::type>>::value ));
+}
+
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);