--- /dev/null
+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 )
--- /dev/null
+Subproject commit 3447fc31b4eea1fbcb86fa0e2f5d9ed9f38776bf
--- /dev/null
+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)
--- /dev/null
+#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;
+}
+
--- /dev/null
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+int solution(int A, int B);
+
+#endif // SOLUTION_H_
--- /dev/null
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+
+TEST(SolutionTestSuite, returnCorrectResult)
+{
+ ASSERT_EQ(3, solution(4,17));
+ ASSERT_EQ(5, solution(-4,17));
+}
--- /dev/null
+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)
--- /dev/null
+#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;
+}
--- /dev/null
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+int solution(int *A, int A_length);
+
+#endif // SOLUTION_H_
--- /dev/null
+#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
+}
--- /dev/null
+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)
--- /dev/null
+#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<<i);
+ }
+ else
+ {
+ X += A[i]*(1<<i);
+ }
+ }
+ result.N = X + 1;
+ result.B = A;
+
+ for (i = 0; i <= 100; i++)
+ {
+ if (i & 0x1)
+ {
+ sign = -1;
+ }
+ else
+ {
+ sign = 1;
+ }
+ if (X + sign*(1 << i))
+ {
+ }
+ }
+
+ return result;
+}
+
--- /dev/null
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+struct Results {
+ int * B;
+ int N;
+};
+
+struct Results solution(int A[], int M);
+
+#endif // SOLUTION_H_
--- /dev/null
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+TEST(SolutionTestSuite, returnCorrectResult)
+{
+ struct Results res;
+ int A[1000];
+ A[0] = 1;
+ A[1] = 0;
+ A[2] = 1;
+ // X = 5
+
+ res = solution(A, 3);
+
+ ASSERT_EQ(6, res.N);
+// ASSERT_EQ(0, res.B[0]);
+// ASSERT_EQ(1, res.B[1]);
+// ASSERT_EQ(0, res.B[3]);
+// ASSERT_EQ(1, res.B[4]);
+// ASSERT_EQ(1, res.B[5]);
+}
--- /dev/null
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution4 solution.c solution.h unittest.cpp)
+add_test(solution4 solution4)
+target_link_libraries(solution4 gtest_main)
+add_custom_command(TARGET solution4 POST_BUILD COMMAND solution4)
--- /dev/null
+#include "solution.h"
+
+int solution(int A, int B)
+{
+ int res = -1;
+ int i, j;
+ int digitsA[11] = { 0 };
+ int digitsB[11] = { 0 };
+
+ for (i = 0; i < 10; i++)
+ {
+ digitsA[i] = A % 10;
+ A = A / 10;
+ digitsB[i] = B % 10;
+ B = B / 10;
+ }
+ j = 0;
+ for (i = 0; i < 10;i++)
+ {
+ if (digitsA[j] == digitsB[i] && digitsB[i] != 0)
+ {
+ j++;
+ res = i;
+ }
+ if (digitsA[j] == 0 && digitsB[i] == 0)
+ {
+ return res - j;
+ }
+ }
+
+ return res;
+}
+
--- /dev/null
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+int solution(int A, int B);
+
+#endif // SOLUTION_H_
--- /dev/null
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+TEST(SolutionTest, SomePosition)
+{
+ int A = 53;
+ int B = 1953786;
+
+ EXPECT_EQ(2, solution(A,B));
+}
+
--- /dev/null
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution5 solution.c solution.h unittest.cpp)
+add_test(solution5 solution5)
+target_link_libraries(solution5 gtest_main)
+add_custom_command(TARGET solution5 POST_BUILD COMMAND solution5)
--- /dev/null
+#include "solution.h"
+
+int solution(int A, int B)
+{
+ int res = -1;
+ int i, j, k, l;
+ int digitsA[10] = { 0 };
+ int digitsB[10] = { 0 };
+ int digitsC[10] = { 0 };
+
+ for (i = 0; i < 9; i++)
+ {
+ digitsA[9-i] = A % 10;
+ A = A / 10;
+ digitsB[9-i] = B % 10;
+ B = B / 10;
+ }
+ j = 9;
+ k = 9;
+ for (i = 0; i < 9; i++)
+ {
+ if (digitsA[i] != 0 && j == 9)
+ {
+ j = i;
+ }
+ if (digitsB[i] != 0 && k == 9)
+ {
+ k = i;
+ }
+ }
+ l = 0;
+ for (i = 0; i < 9; i++)
+ {
+ if (j < 10)
+ {
+ digitsC[l++] = digitsA[j];
+ j++;
+ }
+ if (k < 10)
+ {
+ digitsC[l++] = digitsB[k];
+ k++;
+ }
+ }
+ res = 0;
+ for (i = 0; i < l; i++)
+ {
+ res *= 10;
+ res += digitsC[i];
+ }
+
+ return res;
+}
+
--- /dev/null
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+
+int solution(int A, int B);
+
+#endif // SOLUTION_H_
--- /dev/null
+#include "gtest/gtest.h"
+extern "C" {
+#include "solution.h"
+}
+
+TEST(SolutionTest, ZipSome)
+{
+ EXPECT_EQ(1526, solution(12,56));
+ EXPECT_EQ(5162, solution(56,12));
+ EXPECT_EQ(16273845, solution(12345,678));
+ EXPECT_EQ(16273890, solution(123,67890));
+ EXPECT_EQ(10234, solution(1234,0));
+}
--- /dev/null
+cmake_minimum_required (VERSION 2.6)
+add_executable(solution6 unittest.cpp solution.c solution.h)
+add_test(solution6 solution6)
+target_link_libraries(solution6 gtest_main)
+add_custom_command(TARGET solution6 POST_BUILD COMMAND solution6)
--- /dev/null
+#include "Solution.h"
+
+int solution(char *S)
+{
+ int stack[10];
+ int top = 0;
+ int i;
+ char *ch=S;
+ int num1;
+ int num2;
+ int digits[8];
+ int num_digits = 0;
+
+ while (*ch != 0)
+ {
+ if (*ch >= '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];
+}
--- /dev/null
+#ifndef SOLUTION_H_
+#define SOLUTION_H_
+
+int solution(char *S);
+
+#endif // SOLUTION_H_
--- /dev/null
+#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));
+}