Skip to content

Commit

Permalink
Add tests. (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvassilev authored Nov 7, 2024
1 parent a2fc4e9 commit 7a670ee
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/compile_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# coding: utf-8

import glob
import os
from os import system
root_tests_dir = '.'

for test_dir in os.listdir(root_tests_dir):
if os.path.isdir(os.path.join(root_tests_dir, test_dir)):
print(test_dir)
if not os.path.exists("{test_dir}/a".format(test_dir = test_dir)):
os.makedirs("{test_dir}/a".format(test_dir = test_dir))

if not os.path.exists("{test_dir}/b".format(test_dir = test_dir)):
os.makedirs("{test_dir}/b".format(test_dir = test_dir))

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??'))))
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??'))))
31 changes: 31 additions & 0 deletions test/df_dynamic_vars_address/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int f(int arg1, int arg2) {
#if V2
int i;
#endif

int var1 = arg1 + 1;
int var2 = arg2 + 2;

#if V1
return var1 + var2;
#else
int result;
for (i = 0; i < 5; i++) {
result = var1 + var2;
}

return result;
#endif

}

int main()
{
std::cout << "Result:" << f(1, 2) << std::endl;
return 0;
}
22 changes: 22 additions & 0 deletions test/dynamic_vars_address/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int f(int arg1, int arg2) {
#if V1
int *var1 = new int(1);
int *var2 = new int(2);
#else
int *var2 = new int(2);
int *var1 = new int(1);
#endif
std::cout << "var1_ptr:" << var1 << ", var2_ptr:" << var2 << std::endl;
return *var1 + *var2 + arg1 + arg2;
}

int main()
{
std::cout << "Result:" << f(1, 2) << std::endl;
return 0;
}
22 changes: 22 additions & 0 deletions test/local_vars_address/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int f(int arg1, int arg2) {
#if V1
int var1 = 1;
int var2 = 2;
#else
int var2 = 2;
int var1 = 1;
#endif
std::cout << "var1_ptr:" << &var1 << ", var2_ptr:" << &var2 << std::endl;
return var1 + var2 + arg1 + arg2;
}

int main()
{
std::cout << "Result:" << f(1, 2) << std::endl;
return 0;
}
21 changes: 21 additions & 0 deletions test/local_vars_switched/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//g++ -DV1 -o a/program.out -xc++ -g program.cxx
//g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int a(int arg1, int arg2) {
#if V1
int a_var1 = 1;
int a_var2 = 2;
#else
int a_var2 = 2;
int a_var1 = 1;
#endif
return a_var1 + a_var2 + arg1 + arg2;
}

int main()
{
std::cout << "Result:" << a(1, 2) << std::endl;
return a(1, 2);
}
18 changes: 18 additions & 0 deletions test/pointer_offset/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int a3(int i = 3) {
int * j = new int(2);
int * k = new int(7);
std::cout << "Ptr:" << j << std::endl;

return 0;
}


int main()
{
return a3();
}
41 changes: 41 additions & 0 deletions test/pointer_simple_class/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

class Task {
public:
int id;
Task *next;
Task(int i, Task *n):
id(i),
next(n)
{}
};

int a3(int i = 3) {
int * j = new int(2);
int * k = new int(7);
std::cout << "Ptr:" << j << std::endl;

return 0;
}


int main()
{
Task *task_head = new Task(-1, NULL);
Task *task1 = new Task(1, NULL);
//Task *task2 = new Task(2, NULL);
//Task *task3 = new Task(3, NULL);
//Task *task4 = new Task(4, NULL);
//Task *task5 = new Task(5, NULL);

task_head->next = task1;
//task1->next = task2;
//task2->next = task3;
//task3->next = task4;
//task4->next = task5;

return 0;
}
21 changes: 21 additions & 0 deletions test/recursive_factorial/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int f(int arg1) {
if (arg1 == 0)
#if V1
return 1;
#else
return 0;
#endif
else
return arg1 * f(arg1 - 1);
}

int main()
{
std::cout << "Result:" << f(5) << std::endl;
return 0;
}
24 changes: 24 additions & 0 deletions test/recursive_fib/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int fib(int x) {
#if V1
if (x < 2) {
return x;
}
#else
if (x <= 1) {
return 1;
}
#endif
return (fib(x - 1) + fib(x - 2));
}

int main()
{
for (int i = 0; i <= 5; i++)
std::cout << "Result fib(" << i << "):" << fib(i) << std::endl;
return 0;
}
35 changes: 35 additions & 0 deletions test/recursive_str/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>
#include <string>

std::string dup(int cnt, std::string x) {
#if V1
if (cnt <= 0) {
return "";
} else {
return x + dup(cnt - 1, x);
}
#else
if (cnt <= 0) {
return "";
} else {
std::string s = dup(cnt/2, x);
return (cnt%1 == 0) ? s + s : s + s + x; // cnt%2 == 0
}
#endif
}

int main()
{
std::cout << "Result(0):" << dup(0, "*") << std::endl;
std::cout << "Result(1):" << dup(1, "*") << std::endl;
std::cout << "Result(2):" << dup(2, "*") << std::endl;
std::cout << "Result(3):" << dup(3, "*") << std::endl;
std::cout << "Result(4):" << dup(4, "*") << std::endl;
std::cout << "Result(5):" << dup(5, "*") << std::endl;
std::cout << "Result(6):" << dup(6, "*") << std::endl;
std::cout << "Result(200):" << dup(200, "*") << std::endl;
return 0;
}
20 changes: 20 additions & 0 deletions test/simple/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int f(int arg1, int arg2) {
int var1 = 1;
#if V1
int var2 = 2;
#else
int var2 = 3;
#endif
return arg1 + arg2 + var1 + var2;
}

int main()
{
std::cout << "Result:" << f(1, 2) << std::endl;
return 0;
}
56 changes: 56 additions & 0 deletions test/simple_functions_chain/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <iostream>

int c(int arg1, int arg2) {
int c_var1 = 100;
int c_var2;
#if V1
c_var2 = 2100;
#else
c_var2 = 2000;
#endif
int c_var3 = 300;
int c_var4 = 400;
int c_var5 = 500;
return c_var4 + c_var5;
}

int b(int arg1, int arg2) {
int b_var1 = 10;
int b_var2;
#if V1
b_var2 = 210;
#else
b_var2 = 200;
#endif
int b_var3 = 30;
int b_var4 = 40;
int b_var5 = 50;
#if V1
return c(b_var4, b_var5);
#else
return c(b_var3, b_var4);
#endif
}

int a(int arg1, int arg2) {
int a_var1 = 1;
int a_var2;
#if V1
a_var2 = 21;
#else
a_var2 = 20;
#endif
int a_var3 = 3;
int a_var4 = 4;
int a_var5 = 5;
return b(a_var4, a_var5);
}

int main()
{
std::cout << "Result:" << a(1, 2) << std::endl;
return 0;
}
22 changes: 22 additions & 0 deletions test/strace_sample/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

#include <stdio.h>
#include <unistd.h>

void file_open()
{
char str[30];
#if V1
FILE *fp = fopen("/tmp/strace_sample", "r");
#else
FILE *fp = fopen("strace_sample", "r");
#endif
fgets(str, 5, fp);
fclose(fp);
}

int main() {
file_open();
return 0;
}
18 changes: 18 additions & 0 deletions test/vectors/program.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// g++ -DV1 -o a/program.out -xc++ -g program.cxx
// g++ -DV2 -o b/program.out -xc++ -g program.cxx

static char array[1000][1000];

int main (void)
{
int i, j;

for (i = 0; i < 1000; i++)
for (j = 0; j < 1000; j++)
#if V1
array[i][j]++;
#else
array[j][i]++;
#endif
return 0;
}

0 comments on commit 7a670ee

Please sign in to comment.