Add test.cpp to test C++ API
This commit is contained in:
		
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@@ -1,16 +1,20 @@
 | 
				
			|||||||
prefix = /usr/local
 | 
					prefix = /usr/local
 | 
				
			||||||
bindir = $(prefix)/bin
 | 
					bindir = $(prefix)/bin
 | 
				
			||||||
CFLAGS = -std=c99 -Wall -Wextra -pedantic
 | 
					CFLAGS = -std=c99 -Wall -Wextra -pedantic
 | 
				
			||||||
 | 
					CXXFLAGS = -std=c++20 -Wall -Wextra -pedantic
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.DEFAULT_GOAL := test
 | 
					.DEFAULT_GOAL := test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
clean:
 | 
					clean:
 | 
				
			||||||
	rm -f test
 | 
						rm -f test testcpp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test: test.c
 | 
					test: test.c
 | 
				
			||||||
	$(CC) test.c -g $(CFLAGS) -o test
 | 
						$(CC) test.c -g $(CFLAGS) -o test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					testcpp: test.cpp
 | 
				
			||||||
 | 
						$(CXX) test.cpp -g $(CXXFLAGS) -o testcpp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
check: test
 | 
					check: test
 | 
				
			||||||
	./test.sh
 | 
						./test.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										80
									
								
								test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								test.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
				
			|||||||
 | 
					#include "exile.hpp"
 | 
				
			||||||
 | 
					#include "assert.h"
 | 
				
			||||||
 | 
					#include <map>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string sandboxed_reverse(std::string str)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::reverse(str.begin(), str.end());
 | 
				
			||||||
 | 
						return str;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::string deserialize_stdstring(const char *buf, size_t n)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return std::string { buf, n };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					size_t serialize_stdstring(const std::string &t, char *buf, size_t n)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if(n < t.size())
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						memcpy(buf, t.data(), t.size());
 | 
				
			||||||
 | 
						return t.size();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					size_t stdstrlen(const std::string &str)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return str.size();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int incrementer(int arg)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return ++arg;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					int test_exile_launch_trivial()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int u = 22;
 | 
				
			||||||
 | 
						int result = exile_launch_trivial<int>(exile_init_policy(), &incrementer, u);
 | 
				
			||||||
 | 
						assert(result == 23);
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int test_exile_launch()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						std::string str = "abc123";
 | 
				
			||||||
 | 
						std::string reversed = exile_launch<std::string>(exile_init_policy(), &serialize_stdstring, &deserialize_stdstring, &sandboxed_reverse, str);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						assert(reversed == "321cba");
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(int argc, char *argv[])
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if(argc < 2)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							std::cerr << "Missing test" << std::endl;
 | 
				
			||||||
 | 
							return 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						std::map<std::string, int (*)()> map = {
 | 
				
			||||||
 | 
							{ "launch-trivial-cpp", &test_exile_launch_trivial} ,
 | 
				
			||||||
 | 
							{ "launch-cpp", &test_exile_launch }
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						std::string test = argv[1];
 | 
				
			||||||
 | 
						if(test == "--dumptests")
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							for(auto &entry : map)
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								std::cout << entry.first << std::endl;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						int (*fn)() = map[test];
 | 
				
			||||||
 | 
						if(fn != nullptr)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							return fn();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						std::cerr << "Unknown test" << std::endl;
 | 
				
			||||||
 | 
						return 1;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user