From d26a63601ddb281562a0559224406949691577f3 Mon Sep 17 00:00:00 2001 From: Jannik ZANDER Date: Thu, 3 Nov 2016 23:46:20 +0100 Subject: [PATCH 1/1] Initial commit --- CMakeLists.txt | 22 ++++++++++++++ googletest | 1 + solution1/CMakeLists.txt | 5 ++++ solution1/solution.c | 16 ++++++++++ solution1/solution.h | 7 +++++ solution1/unittest.cpp | 11 +++++++ solution2/CMakeLists.txt | 5 ++++ solution2/solution.c | 60 +++++++++++++++++++++++++++++++++++++ solution2/solution.h | 6 ++++ solution2/unittest.cpp | 65 ++++++++++++++++++++++++++++++++++++++++ solution3/CMakeLists.txt | 5 ++++ solution3/solution.c | 40 +++++++++++++++++++++++++ solution3/solution.h | 12 ++++++++ solution3/unittest.cpp | 22 ++++++++++++++ solution4/CMakeLists.txt | 5 ++++ solution4/solution.c | 33 ++++++++++++++++++++ solution4/solution.h | 7 +++++ solution4/unitTest.cpp | 13 ++++++++ solution5/CMakeLists.txt | 5 ++++ solution5/solution.c | 54 +++++++++++++++++++++++++++++++++ solution5/solution.h | 7 +++++ solution5/unitTest.cpp | 13 ++++++++ solution6/CMakeLists.txt | 5 ++++ solution6/solution.c | 62 ++++++++++++++++++++++++++++++++++++++ solution6/solution.h | 6 ++++ solution6/unittest.cpp | 14 +++++++++ 26 files changed, 501 insertions(+) create mode 100644 CMakeLists.txt create mode 160000 googletest create mode 100644 solution1/CMakeLists.txt create mode 100644 solution1/solution.c create mode 100644 solution1/solution.h create mode 100644 solution1/unittest.cpp create mode 100644 solution2/CMakeLists.txt create mode 100644 solution2/solution.c create mode 100644 solution2/solution.h create mode 100644 solution2/unittest.cpp create mode 100644 solution3/CMakeLists.txt create mode 100644 solution3/solution.c create mode 100644 solution3/solution.h create mode 100644 solution3/unittest.cpp create mode 100644 solution4/CMakeLists.txt create mode 100644 solution4/solution.c create mode 100644 solution4/solution.h create mode 100644 solution4/unitTest.cpp create mode 100644 solution5/CMakeLists.txt create mode 100644 solution5/solution.c create mode 100644 solution5/solution.h create mode 100644 solution5/unitTest.cpp create mode 100644 solution6/CMakeLists.txt create mode 100644 solution6/solution.c create mode 100644 solution6/solution.h create mode 100644 solution6/unittest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..be43dad --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 2.6) +project(basic_test) + +################################ +# GTest +################################ +# Prevent GoogleTest from overriding our compiler/linker options +# when building with Visual Studio +set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +add_subdirectory(googletest) +enable_testing() +include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) +################################ +# Unit Tests +################################ +# Add test cpp file +add_subdirectory( solution1 ) +add_subdirectory( solution2 ) +add_subdirectory( solution3 ) +add_subdirectory( solution4 ) +add_subdirectory( solution5 ) +add_subdirectory( solution6 ) diff --git a/googletest b/googletest new file mode 160000 index 0000000..3447fc3 --- /dev/null +++ b/googletest @@ -0,0 +1 @@ +Subproject commit 3447fc31b4eea1fbcb86fa0e2f5d9ed9f38776bf diff --git a/solution1/CMakeLists.txt b/solution1/CMakeLists.txt new file mode 100644 index 0000000..dd8e229 --- /dev/null +++ b/solution1/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required (VERSION 2.6) +add_executable(solution1 solution.c solution.h unittest.cpp) +add_test(solution1 solution1) +target_link_libraries(solution1 gtest_main) +add_custom_command(TARGET solution1 POST_BUILD COMMAND solution1) diff --git a/solution1/solution.c b/solution1/solution.c new file mode 100644 index 0000000..1bdf3d0 --- /dev/null +++ b/solution1/solution.c @@ -0,0 +1,16 @@ +#include "solution.h" + +int solution(int A, int B) +{ + int i; + int count = 0; + for (i = 0; i <= 100; i++) + { + if (A <= i*i && i*i <= B) + { + count++; + } + } + return count; +} + diff --git a/solution1/solution.h b/solution1/solution.h new file mode 100644 index 0000000..df89de0 --- /dev/null +++ b/solution1/solution.h @@ -0,0 +1,7 @@ +#ifndef SOLUTION_H_ +#define SOLUTION_H_ + + +int solution(int A, int B); + +#endif // SOLUTION_H_ diff --git a/solution1/unittest.cpp b/solution1/unittest.cpp new file mode 100644 index 0000000..53fcd66 --- /dev/null +++ b/solution1/unittest.cpp @@ -0,0 +1,11 @@ +#include "gtest/gtest.h" +extern "C" { +#include "solution.h" +} + + +TEST(SolutionTestSuite, returnCorrectResult) +{ + ASSERT_EQ(3, solution(4,17)); + ASSERT_EQ(5, solution(-4,17)); +} diff --git a/solution2/CMakeLists.txt b/solution2/CMakeLists.txt new file mode 100644 index 0000000..b8b6dfb --- /dev/null +++ b/solution2/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required (VERSION 2.6) +add_executable(solution2 solution.c solution.h unittest.cpp) +add_test(solution2 solution2) +target_link_libraries(solution2 gtest_main) +add_custom_command(TARGET solution2 POST_BUILD COMMAND solution2) diff --git a/solution2/solution.c b/solution2/solution.c new file mode 100644 index 0000000..7301690 --- /dev/null +++ b/solution2/solution.c @@ -0,0 +1,60 @@ +#include "solution.h" + +int solution_with_bug(int *A, int A_length) +{ + int n = A_length; + int result = 0; + int i; + for (i = 0; i < n - 1; i++) { + if (A[i] == A[i + 1]) + result = result + 1; + } + int r = 0; + for (i = 0; i < n; i++) { + int count = 0; + if (i > 0) { + if (A[i - 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (i < n - 1) { + if (A[i + 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (count > r) + r = count; + } + return result + r; +} + +int solution(int *A, int A_length) { + int n = A_length; + int result = 1; + int i; + for (i = 0; i < n - 1; i++) { + if (A[i] == A[i + 1] && A[i] == 1) + result = result + 1; + } + int r = 0; + for (i = 0; i < n; i++) { + int count = 0; + if (i > 0) { + if (A[i - 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (i < n - 1) { + if (A[i + 1] != A[i]) + count = count + 1; + else + count = count - 1; + } + if (count > r) + r = count; + } + return result + r; +} diff --git a/solution2/solution.h b/solution2/solution.h new file mode 100644 index 0000000..b8ca84b --- /dev/null +++ b/solution2/solution.h @@ -0,0 +1,6 @@ +#ifndef SOLUTION_H_ +#define SOLUTION_H_ + +int solution(int *A, int A_length); + +#endif // SOLUTION_H_ diff --git a/solution2/unittest.cpp b/solution2/unittest.cpp new file mode 100644 index 0000000..0f11933 --- /dev/null +++ b/solution2/unittest.cpp @@ -0,0 +1,65 @@ +#include "gtest/gtest.h" +extern "C" { +#include "solution.h" +} + + +TEST(SolutionTestSuite, returnCorrectResult) +{ + int A[1000]; + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + A[5] = 0; + + ASSERT_EQ(4, solution(A,6)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + A[5] = 0; + A[6] = 0; +#if 0 + ASSERT_EQ(5, solution(A,7)); + + A[0] = 1; + A[1] = 1; + + ASSERT_EQ(2, solution(A,2)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + + ASSERT_EQ(3, solution(A,3)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + + ASSERT_EQ(4, solution(A,4)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + + ASSERT_EQ(4, solution(A,5)); + + A[0] = 1; + A[1] = 1; + A[2] = 0; + A[3] = 1; + A[4] = 0; + A[5] = 0; + + ASSERT_EQ(4, solution(A,6)); +#endif +} diff --git a/solution3/CMakeLists.txt b/solution3/CMakeLists.txt new file mode 100644 index 0000000..5dff733 --- /dev/null +++ b/solution3/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required (VERSION 2.6) +add_executable(solution3 solution.c solution.h unittest.cpp) +add_test(solution1 solution3) +target_link_libraries(solution3 gtest_main) +add_custom_command(TARGET solution3 POST_BUILD COMMAND solution3) diff --git a/solution3/solution.c b/solution3/solution.c new file mode 100644 index 0000000..085b3d6 --- /dev/null +++ b/solution3/solution.c @@ -0,0 +1,40 @@ +#include "solution.h" + +struct Results solution(int A[], int M) +{ + struct Results result; + int i; + int X = 0; + int sign = 0; + for (i = 0; i < M; i++) + { + if (i & 0x1) + { + X -= A[i]*(1<= '0' && *ch <= '9') + { + digits[num_digits++]= *ch - '0'; + } + else if (*ch == ' ' && num_digits) + { + num1 = 0; + for (i = 0; i < num_digits; i++) + { + num1 *= 10; + num1 += digits[i]; + } + num_digits = 0; + stack[top++] = num1; + } + else if (*ch == 'D' && *(ch+1)=='U' && *(ch+2)=='P') + { + if (top < 1) return -1; + num1 = stack[top-1]; + stack[top++] = num1; + } + else if (*ch == 'P' && *(ch+1)=='O' && *(ch+2)=='P') + { + if (top < 1) return -1; + num1 = stack[--top]; + } + else if (*ch=='+') + { + if (top < 2) return -1; + num1 = stack[--top]; + num2 = stack[--top]; + stack[top++] = num1 + num2; + } + else if (*ch=='-') + { + if (top < 2) return -1; + num1 = stack[--top]; + num2 = stack[--top]; + if (num1 - num2 < 0) return -1; + stack[top++] = num1 - num2; + } + ch++; + } + + + return stack[top-1]; +} diff --git a/solution6/solution.h b/solution6/solution.h new file mode 100644 index 0000000..84ad726 --- /dev/null +++ b/solution6/solution.h @@ -0,0 +1,6 @@ +#ifndef SOLUTION_H_ +#define SOLUTION_H_ + +int solution(char *S); + +#endif // SOLUTION_H_ diff --git a/solution6/unittest.cpp b/solution6/unittest.cpp new file mode 100644 index 0000000..8063d85 --- /dev/null +++ b/solution6/unittest.cpp @@ -0,0 +1,14 @@ +#include "gtest/gtest.h" +extern "C" { +#include "solution.h" +} + +TEST(SolutionTest, SomeOperations) +{ + char S1[]= "13 DUP 4 POP 5 DUP + DUP + -"; + char S2[] = "5 6 + -"; +// char S2[] ="3 DUP 5 - -"; + + EXPECT_EQ(7, solution(S1)); + EXPECT_EQ(-1, solution(S2)); +} -- 2.43.0