summaryrefslogtreecommitdiff
path: root/tests/projects/c++/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2019-09-06 22:51:54 +0800
committerruki <[email protected]>2019-09-06 11:11:25 +0800
commit7210427c931d41fd52608ee5a502ea54414d6606 (patch)
treeade539fdb971d8974efcc14e20819fe187601c63 /tests/projects/c++/modules
parent5c0286dd6ec8c3f8bf7e6c4e7cecd9d5903948e8 (diff)
add some c++ module tests
Diffstat (limited to 'tests/projects/c++/modules')
-rw-r--r--tests/projects/c++/modules/class/src/hello.mpp12
-rw-r--r--tests/projects/c++/modules/class/src/hello_impl.cpp16
-rw-r--r--tests/projects/c++/modules/class/src/main.cpp7
-rw-r--r--tests/projects/c++/modules/class/xmake.lua5
-rw-r--r--tests/projects/c++/modules/dependence/src/hello.mpp21
-rw-r--r--tests/projects/c++/modules/dependence/src/hello_impl.cpp23
-rw-r--r--tests/projects/c++/modules/dependence/src/main.cpp9
-rw-r--r--tests/projects/c++/modules/dependence/src/mod.mpp5
-rw-r--r--tests/projects/c++/modules/dependence/src/mod_impl.cpp8
-rw-r--r--tests/projects/c++/modules/dependence/xmake.lua6
-rw-r--r--tests/projects/c++/modules/inline_and_template/src/foo.mpp17
-rw-r--r--tests/projects/c++/modules/inline_and_template/src/foo_impl.cpp8
-rw-r--r--tests/projects/c++/modules/inline_and_template/src/hello.mpp9
-rw-r--r--tests/projects/c++/modules/inline_and_template/src/main.cpp12
-rw-r--r--tests/projects/c++/modules/inline_and_template/src/say.mpp11
-rw-r--r--tests/projects/c++/modules/inline_and_template/xmake.lua6
-rw-r--r--tests/projects/c++/modules/modulemap.modulemap/src/hello.h18
-rw-r--r--tests/projects/c++/modules/modulemap.modulemap/src/main.cpp9
-rw-r--r--tests/projects/c++/modules/modulemap.modulemap/src/module.modulemap4
-rw-r--r--tests/projects/c++/modules/modulemap.modulemap/xmake.lua6
-rw-r--r--tests/projects/c++/modules/template_instantiation/src/main.cpp6
-rw-r--r--tests/projects/c++/modules/template_instantiation/src/mod.mpp78
-rw-r--r--tests/projects/c++/modules/template_instantiation/xmake.lua6
23 files changed, 302 insertions, 0 deletions
diff --git a/tests/projects/c++/modules/class/src/hello.mpp b/tests/projects/c++/modules/class/src/hello.mpp
new file mode 100644
index 000000000..9e4ecd960
--- /dev/null
+++ b/tests/projects/c++/modules/class/src/hello.mpp
@@ -0,0 +1,12 @@
+export module hello;
+
+export namespace hello {
+ class say {
+ public:
+ say(int data);
+ void hello();
+
+ private:
+ int data_;
+ };
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/class/src/hello_impl.cpp b/tests/projects/c++/modules/class/src/hello_impl.cpp
new file mode 100644
index 000000000..b929c453b
--- /dev/null
+++ b/tests/projects/c++/modules/class/src/hello_impl.cpp
@@ -0,0 +1,16 @@
+// module;
+#include <iostream>
+module hello;
+
+// import std.core;
+using namespace std;
+
+namespace hello {
+ say::say(int data) : data_(data) {
+
+ }
+
+ void say::hello() {
+ cout << "hello, say class: " << data_ << endl;
+ }
+}
diff --git a/tests/projects/c++/modules/class/src/main.cpp b/tests/projects/c++/modules/class/src/main.cpp
new file mode 100644
index 000000000..6f80b6c7d
--- /dev/null
+++ b/tests/projects/c++/modules/class/src/main.cpp
@@ -0,0 +1,7 @@
+import hello;
+
+int main() {
+ hello::say s(sizeof(hello::say));
+ s.hello();
+ return 0;
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/class/xmake.lua b/tests/projects/c++/modules/class/xmake.lua
new file mode 100644
index 000000000..f0dcc3ea7
--- /dev/null
+++ b/tests/projects/c++/modules/class/xmake.lua
@@ -0,0 +1,5 @@
+target("class")
+ set_kind("binary")
+ add_files("src/*.cpp", "src/*.mpp")
+
+
diff --git a/tests/projects/c++/modules/dependence/src/hello.mpp b/tests/projects/c++/modules/dependence/src/hello.mpp
new file mode 100644
index 000000000..b3153a5b3
--- /dev/null
+++ b/tests/projects/c++/modules/dependence/src/hello.mpp
@@ -0,0 +1,21 @@
+export module hello;
+
+export namespace hello {
+ extern int data__;
+ void say_hello();
+
+ class say {
+ public:
+ say(int data);
+ void hello();
+
+ private:
+ int data_;
+ };
+}
+
+export namespace {
+ void anonymous() {
+
+ }
+}
diff --git a/tests/projects/c++/modules/dependence/src/hello_impl.cpp b/tests/projects/c++/modules/dependence/src/hello_impl.cpp
new file mode 100644
index 000000000..37797c31c
--- /dev/null
+++ b/tests/projects/c++/modules/dependence/src/hello_impl.cpp
@@ -0,0 +1,23 @@
+// module;
+#include <iostream>
+module hello;
+import mod;
+
+void inner() {
+ std::cout << "hello world! data: "
+ << mod::foo() << std::endl;
+}
+
+namespace hello {
+ int data__;
+ void say_hello() { ::inner(); }
+
+ say::say(int data) : data_{data} {
+
+ }
+
+ void say::hello() {
+ hello::data__ = data_;
+ ::inner();
+ }
+}
diff --git a/tests/projects/c++/modules/dependence/src/main.cpp b/tests/projects/c++/modules/dependence/src/main.cpp
new file mode 100644
index 000000000..93cc427a4
--- /dev/null
+++ b/tests/projects/c++/modules/dependence/src/main.cpp
@@ -0,0 +1,9 @@
+import hello;
+
+int main() {
+ hello::data__ = 123;
+ hello::say_hello();
+ hello::say(sizeof(hello::say)).hello();
+ return 0;
+}
+
diff --git a/tests/projects/c++/modules/dependence/src/mod.mpp b/tests/projects/c++/modules/dependence/src/mod.mpp
new file mode 100644
index 000000000..ac30a31c8
--- /dev/null
+++ b/tests/projects/c++/modules/dependence/src/mod.mpp
@@ -0,0 +1,5 @@
+export module mod;
+
+export namespace mod {
+ int foo();
+}
diff --git a/tests/projects/c++/modules/dependence/src/mod_impl.cpp b/tests/projects/c++/modules/dependence/src/mod_impl.cpp
new file mode 100644
index 000000000..1adf6570e
--- /dev/null
+++ b/tests/projects/c++/modules/dependence/src/mod_impl.cpp
@@ -0,0 +1,8 @@
+module mod;
+import hello;
+
+namespace mod {
+ int foo() {
+ return hello::data__;
+ }
+}
diff --git a/tests/projects/c++/modules/dependence/xmake.lua b/tests/projects/c++/modules/dependence/xmake.lua
new file mode 100644
index 000000000..4a2608cd3
--- /dev/null
+++ b/tests/projects/c++/modules/dependence/xmake.lua
@@ -0,0 +1,6 @@
+target("dependence")
+ set_kind("binary")
+ add_files("src/*.cpp", "src/*.mpp")
+ set_languages("c++11")
+
+
diff --git a/tests/projects/c++/modules/inline_and_template/src/foo.mpp b/tests/projects/c++/modules/inline_and_template/src/foo.mpp
new file mode 100644
index 000000000..03e15d413
--- /dev/null
+++ b/tests/projects/c++/modules/inline_and_template/src/foo.mpp
@@ -0,0 +1,17 @@
+export module foo;
+
+export {
+ template <typename>
+ struct foo {
+ constexpr const char* hello(void) const {
+ return "hello typename!";
+ }
+ };
+
+ // template <>
+ // struct foo<int> {
+ // constexpr const char* hello(void) const {
+ // return "hello typename int!";
+ // }
+ // };
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/inline_and_template/src/foo_impl.cpp b/tests/projects/c++/modules/inline_and_template/src/foo_impl.cpp
new file mode 100644
index 000000000..c44ef849b
--- /dev/null
+++ b/tests/projects/c++/modules/inline_and_template/src/foo_impl.cpp
@@ -0,0 +1,8 @@
+module foo;
+
+template <>
+struct foo<int> {
+ constexpr const char* hello(void) const {
+ return "hello int!";
+ }
+}; \ No newline at end of file
diff --git a/tests/projects/c++/modules/inline_and_template/src/hello.mpp b/tests/projects/c++/modules/inline_and_template/src/hello.mpp
new file mode 100644
index 000000000..1796f0b11
--- /dev/null
+++ b/tests/projects/c++/modules/inline_and_template/src/hello.mpp
@@ -0,0 +1,9 @@
+// module;
+#include <cstdio>
+export module hello;
+
+export namespace hello {
+ inline void say_hello() {
+ std::printf("hello world!\n");
+ }
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/inline_and_template/src/main.cpp b/tests/projects/c++/modules/inline_and_template/src/main.cpp
new file mode 100644
index 000000000..a34667fbe
--- /dev/null
+++ b/tests/projects/c++/modules/inline_and_template/src/main.cpp
@@ -0,0 +1,12 @@
+import hello;
+import say;
+import foo;
+
+#include <iostream>
+
+int main() {
+ hello::say_hello();
+ say{}.hello<sizeof(say)>();
+ std::cout << foo<int>{}.hello() << std::endl;
+ return 0;
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/inline_and_template/src/say.mpp b/tests/projects/c++/modules/inline_and_template/src/say.mpp
new file mode 100644
index 000000000..d09e14d28
--- /dev/null
+++ b/tests/projects/c++/modules/inline_and_template/src/say.mpp
@@ -0,0 +1,11 @@
+// module;
+#include <cstdio>
+export module say;
+
+export class say {
+public:
+ template <int N>
+ void hello() {
+ std::printf("hello, say class: %d\n", N);
+ }
+}; \ No newline at end of file
diff --git a/tests/projects/c++/modules/inline_and_template/xmake.lua b/tests/projects/c++/modules/inline_and_template/xmake.lua
new file mode 100644
index 000000000..e22a0ef04
--- /dev/null
+++ b/tests/projects/c++/modules/inline_and_template/xmake.lua
@@ -0,0 +1,6 @@
+target("inline_and_template")
+ set_kind("binary")
+ add_files("src/*.cpp", "src/*.mpp")
+ set_languages("c++11")
+
+
diff --git a/tests/projects/c++/modules/modulemap.modulemap/src/hello.h b/tests/projects/c++/modules/modulemap.modulemap/src/hello.h
new file mode 100644
index 000000000..743934c26
--- /dev/null
+++ b/tests/projects/c++/modules/modulemap.modulemap/src/hello.h
@@ -0,0 +1,18 @@
+#pragma once
+
+import std;
+using namespace std;
+
+namespace hello {
+ inline void say_hi() {
+ cout << "hello hi!" << endl;
+ }
+
+ inline void say_hello() {
+ cout << "hello world!" << endl;
+ }
+
+ inline void say_xz() {
+ cout << "hello xz!" << endl;
+ }
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/modulemap.modulemap/src/main.cpp b/tests/projects/c++/modules/modulemap.modulemap/src/main.cpp
new file mode 100644
index 000000000..6fffc01f7
--- /dev/null
+++ b/tests/projects/c++/modules/modulemap.modulemap/src/main.cpp
@@ -0,0 +1,9 @@
+// #include "hello.h"
+import hello;
+
+int main() {
+ hello::say_hello();
+ hello::say_xz();
+ hello::say_hi();
+ return 0;
+} \ No newline at end of file
diff --git a/tests/projects/c++/modules/modulemap.modulemap/src/module.modulemap b/tests/projects/c++/modules/modulemap.modulemap/src/module.modulemap
new file mode 100644
index 000000000..69c787ee2
--- /dev/null
+++ b/tests/projects/c++/modules/modulemap.modulemap/src/module.modulemap
@@ -0,0 +1,4 @@
+module hello {
+ header "hello.h"
+ export *
+}
diff --git a/tests/projects/c++/modules/modulemap.modulemap/xmake.lua b/tests/projects/c++/modules/modulemap.modulemap/xmake.lua
new file mode 100644
index 000000000..979f7a22e
--- /dev/null
+++ b/tests/projects/c++/modules/modulemap.modulemap/xmake.lua
@@ -0,0 +1,6 @@
+target("modulemap")
+ set_kind("binary")
+ add_files("src/*.cpp", "src/*.mpp")
+ set_languages("c++20")
+
+
diff --git a/tests/projects/c++/modules/template_instantiation/src/main.cpp b/tests/projects/c++/modules/template_instantiation/src/main.cpp
new file mode 100644
index 000000000..babfe1222
--- /dev/null
+++ b/tests/projects/c++/modules/template_instantiation/src/main.cpp
@@ -0,0 +1,6 @@
+import mod;
+
+int main() {
+ test_performance(foo<1000>{}, foo<1000>{});
+ return 0;
+}
diff --git a/tests/projects/c++/modules/template_instantiation/src/mod.mpp b/tests/projects/c++/modules/template_instantiation/src/mod.mpp
new file mode 100644
index 000000000..019f923d7
--- /dev/null
+++ b/tests/projects/c++/modules/template_instantiation/src/mod.mpp
@@ -0,0 +1,78 @@
+export module mod;
+
+class thread {
+public:
+ thread(...) {}
+ void join() {}
+};
+
+class atomic_int {
+public:
+ atomic_int(...) {}
+ int operator++() { return 0; }
+};
+
+/* gcc would ICE with template */
+
+template <typename T>
+class msg_queue {
+public:
+ void put(...) {}
+ T take() { return {}; }
+};
+
+export template <int N, int M, int Loops = 1000000>
+void test_prod_cons() {
+ thread producers[N];
+ thread consumers[M];
+
+ struct msg_t {
+ int pid_;
+ int dat_;
+ };
+ msg_queue<msg_t> messages;
+
+ int cid = 0;
+ for (auto& t : consumers) {
+ t = thread{[&, cid] {
+ while (1) {
+ msg_t msg = messages.take();
+ if (msg.pid_ < 0) break;
+ }
+ }};
+ ++cid;
+ }
+
+ int pid = 0;
+ for (auto& t : producers) {
+ t = thread{[&, pid] {
+ for (int i = 0; i < Loops; ++i) {
+ messages.put(pid, i); // emplace_back
+ }
+ }};
+ ++pid;
+ }
+ for (auto& t : producers) t.join();
+ // quit
+ messages.put(-1, -1);
+ for (auto& t : consumers) t.join();
+}
+
+export template <int N>
+struct foo {};
+
+export inline void test_performance(foo<1>, foo<1>) {
+ test_prod_cons<1, 1>();
+}
+
+export template <int N>
+void test_performance(foo<N>, foo<1>) {
+ test_performance(foo<N - 1>{}, foo<1>{});
+ test_prod_cons<N, 1>();
+};
+
+export template <int N, int M>
+void test_performance(foo<N>, foo<M>) {
+ test_performance(foo<N>{}, foo<M - 1>{});
+ test_prod_cons<N, M>();
+};
diff --git a/tests/projects/c++/modules/template_instantiation/xmake.lua b/tests/projects/c++/modules/template_instantiation/xmake.lua
new file mode 100644
index 000000000..312703cf7
--- /dev/null
+++ b/tests/projects/c++/modules/template_instantiation/xmake.lua
@@ -0,0 +1,6 @@
+target("template_instantiation")
+ set_kind("binary")
+ add_files("src/*.cpp", "src/*.mpp")
+ set_languages("c++20")
+
+