Skip to content

Commit

Permalink
feat: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrothenberger committed Apr 11, 2024
1 parent 95df276 commit 8a0a3c8
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: clean prog

prog: code.o
$(CXX) -o prog code.o $(CXXFLAGS)

code.o:
$(CXX) -c -o code.o code.cpp $(CXXFLAGS)

clean:
rm -rf .discopop
rm -rf src/.discopop
find . -not -name code.cpp -not -name Makefile -not -path **/FileMapping.txt -delete

veryclean: clean
rm -f FileMapping.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char* argv[]) {
static int n = 50000;
double *x = (double *) malloc(n * sizeof(double));
double *y = (double *) malloc(n * sizeof(double));
// Initialize x, y
// Do-All
for(int i = 0; i < n; ++i){
x[i] = 1.0;
y[i] = 2.0;
}

// Not Do-All
for(int i = 0; i < n/10; i++){
// Do-All
for(int j = 0; j < 10; j++){
y[i*10 + j] = x[i*10 + j] + y[i*9 +j];
}
}
free(x);
free(y);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import pathlib
import unittest

import jsonpickle

from discopop_library.result_classes.DetectionResult import DetectionResult
from test.utils.subprocess_wrapper.command_execution_wrapper import run_cmd
from test.utils.validator_classes.DoAllInfoForValidation import DoAllInfoForValidation
from discopop_library.ConfigProvider.config_provider import run as run_config_provider
from discopop_library.ConfigProvider.ConfigProviderArguments import ConfigProviderArguments


class TestMethods(unittest.TestCase):
def test(self):
current_dir = pathlib.Path(__file__).parent.resolve()
dp_build_dir = run_config_provider(
ConfigProviderArguments(
return_dp_build_dir=True,
return_dp_source_dir=False,
return_llvm_bin_dir=False,
return_version_string=False,
)
)

env_vars = dict(os.environ)

src_dir = os.path.join(current_dir, "src")

# create FileMapping
cmd = os.path.join(dp_build_dir, "scripts", "dp-fmap")
run_cmd(cmd, src_dir, env_vars)

# build
env_vars["CC"] = os.path.join(dp_build_dir, "scripts", "CC_wrapper.sh")
env_vars["CXX"] = os.path.join(dp_build_dir, "scripts", "CXX_wrapper.sh")
cmd = "make"
run_cmd(cmd, src_dir, env_vars)
# execute instrumented program
cmd = "./prog"
run_cmd(cmd, src_dir, env_vars)
# execute DiscoPoP analysis
cwd = os.path.join(src_dir, ".discopop")
run_cmd("discopop_explorer", cwd, env_vars)
run_cmd("discopop_optimizer -o3", cwd, env_vars)
# validate results
try:
self.validate_results(current_dir, src_dir)
# clean environment
run_cmd("make veryclean", src_dir, env_vars)
except Exception as ex:
# clean environment
run_cmd("make veryclean", src_dir, env_vars)
raise ex

def validate_results(self, test_dir, src_dir):
"""Check that not collapse has been identified"""

# load test output
test_output_file = os.path.join(src_dir, ".discopop", "optimizer", "detection_result_dump.json")
with open(test_output_file, "r") as f:
tmp_str = f.read()
test_output: DetectionResult = jsonpickle.decode(tmp_str)

# check identified DoAllInfo objects for collapse clauses > 1
for do_all_info in test_output.patterns.do_all:
self.assertTrue(do_all_info.collapse_level <= 1)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
all: clean prog

prog: code.o
$(CXX) -o prog code.o $(CXXFLAGS)

code.o:
$(CXX) -c -o code.o code.cpp $(CXXFLAGS)

clean:
rm -rf .discopop
rm -rf src/.discopop
find . -not -name code.cpp -not -name Makefile -not -path **/FileMapping.txt -delete

veryclean: clean
rm -f FileMapping.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdlib.h>
#include <stdio.h>

int main(int argc, const char* argv[]) {
static int n = 5000;
double *x = (double *) malloc(n * sizeof(double));
double *y = (double *) malloc(n * sizeof(double));
// Initialize x, y
// Do-All
for(int i = 0; i < n; ++i){
x[i] = 1.0;
y[i] = 2.0;
}

// Do-All
for(int i = 0; i < n/10; i++){
// Not Do-All
for(int j = 0; j < 100; j++){
y[i*10 + j] = x[i*10 + j] + y[i*10 +((j+2)%10)];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import pathlib
import unittest

import jsonpickle

from discopop_library.result_classes.DetectionResult import DetectionResult
from test.utils.subprocess_wrapper.command_execution_wrapper import run_cmd
from test.utils.validator_classes.DoAllInfoForValidation import DoAllInfoForValidation
from discopop_library.ConfigProvider.config_provider import run as run_config_provider
from discopop_library.ConfigProvider.ConfigProviderArguments import ConfigProviderArguments


class TestMethods(unittest.TestCase):
def test(self):
current_dir = pathlib.Path(__file__).parent.resolve()
dp_build_dir = run_config_provider(
ConfigProviderArguments(
return_dp_build_dir=True,
return_dp_source_dir=False,
return_llvm_bin_dir=False,
return_version_string=False,
)
)

env_vars = dict(os.environ)

src_dir = os.path.join(current_dir, "src")

# create FileMapping
cmd = os.path.join(dp_build_dir, "scripts", "dp-fmap")
run_cmd(cmd, src_dir, env_vars)

# build
env_vars["CC"] = os.path.join(dp_build_dir, "scripts", "CC_wrapper.sh")
env_vars["CXX"] = os.path.join(dp_build_dir, "scripts", "CXX_wrapper.sh")
cmd = "make"
run_cmd(cmd, src_dir, env_vars)
# execute instrumented program
cmd = "./prog"
run_cmd(cmd, src_dir, env_vars)
# execute DiscoPoP analysis
cwd = os.path.join(src_dir, ".discopop")
run_cmd("discopop_explorer", cwd, env_vars)
run_cmd("discopop_optimizer -o3", cwd, env_vars)
# validate results
try:
self.validate_results(current_dir, src_dir)
# clean environment
run_cmd("make veryclean", src_dir, env_vars)
except Exception as ex:
# clean environment
run_cmd("make veryclean", src_dir, env_vars)
raise ex

def validate_results(self, test_dir, src_dir):
"""Check that not collapse has been identified"""

# load test output
test_output_file = os.path.join(src_dir, ".discopop", "optimizer", "detection_result_dump.json")
with open(test_output_file, "r") as f:
tmp_str = f.read()
test_output: DetectionResult = jsonpickle.decode(tmp_str)

# check identified DoAllInfo objects for collapse clauses > 1
for do_all_info in test_output.patterns.do_all:
self.assertTrue(do_all_info.collapse_level <= 1)

0 comments on commit 8a0a3c8

Please sign in to comment.