Skip to content

Commit 7a670ee

Browse files
authored
Add tests. (#39)
1 parent a2fc4e9 commit 7a670ee

File tree

14 files changed

+370
-0
lines changed

14 files changed

+370
-0
lines changed

test/compile_tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
import glob
5+
import os
6+
from os import system
7+
root_tests_dir = '.'
8+
9+
for test_dir in os.listdir(root_tests_dir):
10+
if os.path.isdir(os.path.join(root_tests_dir, test_dir)):
11+
print(test_dir)
12+
if not os.path.exists("{test_dir}/a".format(test_dir = test_dir)):
13+
os.makedirs("{test_dir}/a".format(test_dir = test_dir))
14+
15+
if not os.path.exists("{test_dir}/b".format(test_dir = test_dir)):
16+
os.makedirs("{test_dir}/b".format(test_dir = test_dir))
17+
18+
system("g++ -DV1 -o {test_d}/a/program.out -xc++ -g {test_files}".format(test_d = test_dir, test_files = ' '.join(glob.glob(test_dir + '/*.c??'))))
19+
system("g++ -DV2 -o {test_d}/b/program.out -xc++ -g {test_files}".format(test_d = test_dir, test_files = ' '.join(glob.glob(test_dir + '/*.c??'))))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int f(int arg1, int arg2) {
7+
#if V2
8+
int i;
9+
#endif
10+
11+
int var1 = arg1 + 1;
12+
int var2 = arg2 + 2;
13+
14+
#if V1
15+
return var1 + var2;
16+
#else
17+
int result;
18+
for (i = 0; i < 5; i++) {
19+
result = var1 + var2;
20+
}
21+
22+
return result;
23+
#endif
24+
25+
}
26+
27+
int main()
28+
{
29+
std::cout << "Result:" << f(1, 2) << std::endl;
30+
return 0;
31+
}

test/dynamic_vars_address/program.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int f(int arg1, int arg2) {
7+
#if V1
8+
int *var1 = new int(1);
9+
int *var2 = new int(2);
10+
#else
11+
int *var2 = new int(2);
12+
int *var1 = new int(1);
13+
#endif
14+
std::cout << "var1_ptr:" << var1 << ", var2_ptr:" << var2 << std::endl;
15+
return *var1 + *var2 + arg1 + arg2;
16+
}
17+
18+
int main()
19+
{
20+
std::cout << "Result:" << f(1, 2) << std::endl;
21+
return 0;
22+
}

test/local_vars_address/program.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int f(int arg1, int arg2) {
7+
#if V1
8+
int var1 = 1;
9+
int var2 = 2;
10+
#else
11+
int var2 = 2;
12+
int var1 = 1;
13+
#endif
14+
std::cout << "var1_ptr:" << &var1 << ", var2_ptr:" << &var2 << std::endl;
15+
return var1 + var2 + arg1 + arg2;
16+
}
17+
18+
int main()
19+
{
20+
std::cout << "Result:" << f(1, 2) << std::endl;
21+
return 0;
22+
}

test/local_vars_switched/program.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
//g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int a(int arg1, int arg2) {
7+
#if V1
8+
int a_var1 = 1;
9+
int a_var2 = 2;
10+
#else
11+
int a_var2 = 2;
12+
int a_var1 = 1;
13+
#endif
14+
return a_var1 + a_var2 + arg1 + arg2;
15+
}
16+
17+
int main()
18+
{
19+
std::cout << "Result:" << a(1, 2) << std::endl;
20+
return a(1, 2);
21+
}

test/pointer_offset/program.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int a3(int i = 3) {
7+
int * j = new int(2);
8+
int * k = new int(7);
9+
std::cout << "Ptr:" << j << std::endl;
10+
11+
return 0;
12+
}
13+
14+
15+
int main()
16+
{
17+
return a3();
18+
}

test/pointer_simple_class/program.cxx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
class Task {
7+
public:
8+
int id;
9+
Task *next;
10+
Task(int i, Task *n):
11+
id(i),
12+
next(n)
13+
{}
14+
};
15+
16+
int a3(int i = 3) {
17+
int * j = new int(2);
18+
int * k = new int(7);
19+
std::cout << "Ptr:" << j << std::endl;
20+
21+
return 0;
22+
}
23+
24+
25+
int main()
26+
{
27+
Task *task_head = new Task(-1, NULL);
28+
Task *task1 = new Task(1, NULL);
29+
//Task *task2 = new Task(2, NULL);
30+
//Task *task3 = new Task(3, NULL);
31+
//Task *task4 = new Task(4, NULL);
32+
//Task *task5 = new Task(5, NULL);
33+
34+
task_head->next = task1;
35+
//task1->next = task2;
36+
//task2->next = task3;
37+
//task3->next = task4;
38+
//task4->next = task5;
39+
40+
return 0;
41+
}

test/recursive_factorial/program.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int f(int arg1) {
7+
if (arg1 == 0)
8+
#if V1
9+
return 1;
10+
#else
11+
return 0;
12+
#endif
13+
else
14+
return arg1 * f(arg1 - 1);
15+
}
16+
17+
int main()
18+
{
19+
std::cout << "Result:" << f(5) << std::endl;
20+
return 0;
21+
}

test/recursive_fib/program.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
6+
int fib(int x) {
7+
#if V1
8+
if (x < 2) {
9+
return x;
10+
}
11+
#else
12+
if (x <= 1) {
13+
return 1;
14+
}
15+
#endif
16+
return (fib(x - 1) + fib(x - 2));
17+
}
18+
19+
int main()
20+
{
21+
for (int i = 0; i <= 5; i++)
22+
std::cout << "Result fib(" << i << "):" << fib(i) << std::endl;
23+
return 0;
24+
}

test/recursive_str/program.cxx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
2+
// g++ -DV2 -o b/program.out -xc++ -g program.cxx
3+
4+
#include <iostream>
5+
#include <string>
6+
7+
std::string dup(int cnt, std::string x) {
8+
#if V1
9+
if (cnt <= 0) {
10+
return "";
11+
} else {
12+
return x + dup(cnt - 1, x);
13+
}
14+
#else
15+
if (cnt <= 0) {
16+
return "";
17+
} else {
18+
std::string s = dup(cnt/2, x);
19+
return (cnt%1 == 0) ? s + s : s + s + x; // cnt%2 == 0
20+
}
21+
#endif
22+
}
23+
24+
int main()
25+
{
26+
std::cout << "Result(0):" << dup(0, "*") << std::endl;
27+
std::cout << "Result(1):" << dup(1, "*") << std::endl;
28+
std::cout << "Result(2):" << dup(2, "*") << std::endl;
29+
std::cout << "Result(3):" << dup(3, "*") << std::endl;
30+
std::cout << "Result(4):" << dup(4, "*") << std::endl;
31+
std::cout << "Result(5):" << dup(5, "*") << std::endl;
32+
std::cout << "Result(6):" << dup(6, "*") << std::endl;
33+
std::cout << "Result(200):" << dup(200, "*") << std::endl;
34+
return 0;
35+
}

0 commit comments

Comments
 (0)