Skip to content

Commit

Permalink
Basic ETE tests
Browse files Browse the repository at this point in the history
Resolve #82
  • Loading branch information
fushar committed Nov 5, 2016
1 parent fbbb276 commit f5c184b
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ install:
- sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-4.7 90

script:
- cmake . && cmake --build . && ./all_tests
- cmake . && cmake --build . && ./tests && ./ete_tests

after_success:
- bash <(curl -s https://codecov.io/bash)
29 changes: 26 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ include_directories("${source_dir}/googlemock/include")

include_directories(include)

set(SOURCE_FILES
set(SOURCES
include/tcframe/experimental/runner.hpp
include/tcframe/generator.hpp
include/tcframe/generator/GenerationException.hpp
Expand Down Expand Up @@ -264,11 +264,34 @@ set(SOURCE_FILES
test/tcframe/deprecated/submitter/SubmitterTests.cpp
)

add_executable(all_tests ${SOURCE_FILES})
add_executable(tests ${SOURCES})

target_link_libraries(all_tests
target_link_libraries(tests
gtest
gmock
${CMAKE_THREAD_LIBS_INIT}
gcov
)

set(ETE_SOURCES
ete-test/tcframe/BaseEteTests.cpp
ete-test/tcframe/GenerationEteTests.cpp
)

add_executable(ete_tests ${ETE_SOURCES})

target_link_libraries(ete_tests
gtest
gmock
${CMAKE_THREAD_LIBS_INIT}
gcov
)

add_custom_command(
TARGET ete_tests
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/include $<TARGET_FILE_DIR:ete_tests>/tcframe/include
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/src $<TARGET_FILE_DIR:ete_tests>/tcframe/src
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/scripts $<TARGET_FILE_DIR:ete_tests>/tcframe/scripts
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/ete-test/resources $<TARGET_FILE_DIR:ete_tests>/ete
)
52 changes: 52 additions & 0 deletions ete-test/resources/multi/multi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <tcframe/experimental/runner.hpp>

using namespace tcframe;

class ProblemSpec : public BaseProblemSpec {
protected:
int T;
int A, B;
int res;

void InputFormat() {
LINE(A, B);
}

void OutputFormat() {
LINE(res);
}

void Config() {
MultipleTestCasesCount(T);
}

void MultipleTestCasesConstraints() {
CONS(1 <= T && T <= 5);
}

void Constraints() {
CONS(1 <= A && A <= 100);
CONS(1 <= B && B <= 100);
}
};

class TestSpec : public BaseTestSpec<ProblemSpec> {
protected:
void SampleTestCase1() {
Input({
"1 5"
});
Output({
"6"
});
}

void TestGroup1() {
CASE(A = 1, B = 3);
CASE(A = 2, B = 4);
}

void TestGroup2() {
CASE(A = 20, B = 30);
}
};
2 changes: 2 additions & 0 deletions ete-test/resources/multi/multi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
timeLimit: 3
memoryLimit: 128
13 changes: 13 additions & 0 deletions ete-test/resources/multi/multi_solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <cstdio>

int T;
int A, B;

int main() {
scanf("%d", &T);

for (int tc = 0; tc < T; tc++) {
scanf("%d %d", &A, &B);
printf("%d\n", A + B);
}
}
39 changes: 39 additions & 0 deletions ete-test/resources/normal/normal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <tcframe/experimental/runner.hpp>

using namespace tcframe;

class ProblemSpec : public BaseProblemSpec {
protected:
int A, B;
int res;

void InputFormat() {
LINE(A, B);
}

void OutputFormat() {
LINE(res);
}

void Constraints() {
CONS(1 <= A && A <= 10);
CONS(1 <= B && B <= 10);
}
};

class TestSpec : public BaseTestSpec<ProblemSpec> {
protected:
void SampleTestCase1() {
Input({
"1 5"
});
Output({
"6"
});
}

void TestCases() {
CASE(A = 1, B = 3);
CASE(A = 2, B = 4);
}
};
2 changes: 2 additions & 0 deletions ete-test/resources/normal/normal.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
timeLimit: 3
memoryLimit: 128
8 changes: 8 additions & 0 deletions ete-test/resources/normal/normal_solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <cstdio>

int A, B;

int main() {
scanf("%d %d", &A, &B);
printf("%d\n", A + B);
}
10 changes: 10 additions & 0 deletions ete-test/resources/scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -ex

export TCFRAME_HOME=../../tcframe

slug=$1
g++ -o ${slug}_solution ${slug}_solution.cpp
$TCFRAME_HOME/scripts/tcframe
./${slug} --solution=./${slug}_solution
63 changes: 63 additions & 0 deletions ete-test/resources/subtasks/subtasks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <tcframe/experimental/runner.hpp>

using namespace tcframe;

class ProblemSpec : public BaseProblemSpec {
protected:
int A, B;
int res;

void InputFormat() {
LINE(A, B);
}

void OutputFormat() {
LINE(res);
}

void Subtask1() {
CONS(1 <= A && A <= 10);
CONS(1 <= B && B <= 10);
}

void Subtask2() {
CONS(1 <= A && A <= 100);
CONS(1 <= B && B <= 100);
}
};

class TestSpec : public BaseTestSpec<ProblemSpec> {
protected:
void SampleTestCase1() {
Subtasks({1, 2});
Input({
"1 5"
});
Output({
"6"
});
}

void SampleTestCase2() {
Subtasks({2});
Input({
"11 12"
});
Output({
"23"
});
}

void TestGroup1() {
Subtasks({1, 2});

CASE(A = 1, B = 3);
CASE(A = 2, B = 4);
}

void TestGroup2() {
Subtasks({2});

CASE(A = 20, B = 30);
}
};
2 changes: 2 additions & 0 deletions ete-test/resources/subtasks/subtasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
timeLimit: 3
memoryLimit: 128
8 changes: 8 additions & 0 deletions ete-test/resources/subtasks/subtasks_solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <cstdio>

int A, B;

int main() {
scanf("%d %d", &A, &B);
printf("%d\n", A + B);
}
48 changes: 48 additions & 0 deletions ete-test/tcframe/BaseEteTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "gmock/gmock.h"

#include <cstdlib>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>

using ::testing::Test;

using std::istringstream;
using std::shared_ptr;
using std::string;
using std::vector;

namespace tcframe {

class BaseEteTests : public Test {
protected:
int execStatus(const string& command) {
return system(command.c_str());
}

string exec(const string& command) {
char buf[128];
string res;
shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
while (!feof(pipe.get())) {
if (fgets(buf, 128, pipe.get()) != nullptr) {
res += buf;
}
}
return res;
}

vector<string> ls(const string& dir) {
istringstream sin(exec("ls -1 " + dir));
vector<string> files;
string file;
while (sin >> file) {
files.push_back(file);
}
return files;
}
};

}
56 changes: 56 additions & 0 deletions ete-test/tcframe/GenerationEteTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "gmock/gmock.h"

#include "BaseEteTests.cpp"

using ::testing::Eq;
using ::testing::Test;
using ::testing::UnorderedElementsAre;

namespace tcframe {

class GenerationEteTests : public BaseEteTests {};

TEST_F(GenerationEteTests, Normal) {
ASSERT_THAT(execStatus("cd ete/normal && ../scripts/generate.sh normal"), Eq(0));

EXPECT_THAT(ls("ete/normal/tc"), UnorderedElementsAre(
"normal_sample_1.in",
"normal_sample_1.out",
"normal_1.in",
"normal_1.out",
"normal_2.in",
"normal_2.out"
));
}

TEST_F(GenerationEteTests, Subtasks) {
ASSERT_THAT(execStatus("cd ete/subtasks && ../scripts/generate.sh subtasks"), Eq(0));

EXPECT_THAT(ls("ete/subtasks/tc"), UnorderedElementsAre(
"subtasks_sample_1.in",
"subtasks_sample_1.out",
"subtasks_sample_2.in",
"subtasks_sample_2.out",
"subtasks_1_1.in",
"subtasks_1_1.out",
"subtasks_1_2.in",
"subtasks_1_2.out",
"subtasks_2_1.in",
"subtasks_2_1.out"
));
}

TEST_F(GenerationEteTests, Multi) {
ASSERT_THAT(execStatus("cd ete/multi && ../scripts/generate.sh multi"), Eq(0));

EXPECT_THAT(ls("ete/multi/tc"), UnorderedElementsAre(
"multi_sample.in",
"multi_sample.out",
"multi_1.in",
"multi_1.out",
"multi_2.in",
"multi_2.out"
));
}

}

0 comments on commit f5c184b

Please sign in to comment.