浏览代码

Working tests for (some of) utils.

Steve Thielemann 4 年之前
父节点
当前提交
7dd34da986
共有 4 个文件被更改,包括 142 次插入7 次删除
  1. 14 6
      CMakeLists.txt
  2. 0 0
      test-lastseen.cpp
  3. 113 0
      test-utils.cpp
  4. 15 1
      utils.cpp

+ 14 - 6
CMakeLists.txt

@@ -42,13 +42,21 @@ set(CMAKE_CXX_EXTENSIONS OFF)
 
 add_subdirectory(googletest)
 
-add_executable(test_lastseen test_lastseen.cpp lastseen.cpp)
-add_dependencies(test_lastseen gtest)
-target_link_libraries(test_lastseen gtest_main)
+### TESTS
+add_executable(test-lastseen test-lastseen.cpp lastseen.cpp)
+add_dependencies(test-lastseen gtest)
+target_link_libraries(test-lastseen gtest_main)
 
 enable_testing()
-add_test(NAME test_lastseen
-  COMMAND test_lastseen)
+add_test(NAME test-lastseen
+  COMMAND test-lastseen)
+
+add_executable(test-utils test-utils.cpp utils.cpp)
+add_dependencies(test-utils gtest)
+target_link_libraries(test-utils gtest_main)
+
+add_test(NAME test-utils
+  COMMAND test-utils)
 
 
 # include(FetchContent)
@@ -81,7 +89,7 @@ add_subdirectory(zf_log)
 add_executable(mystic mystic.cpp lastseen.cpp terminal.cpp render.cpp utils.cpp images.h)
 target_link_libraries(mystic util)
 target_link_libraries(mystic zf_log)
-target_compile_definitions(mystic PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_VERBOSE)
+target_compile_definitions(mystic PUBLIC ZF_LOG_DEF_LEVEL=ZF_LOG_INFO)
 
 add_executable(try-re try-re.c)
 

+ 0 - 0
test_lastseen.cpp → test-lastseen.cpp


+ 113 - 0
test-utils.cpp

@@ -0,0 +1,113 @@
+// Not sure where to begin with gtest?
+//
+// What can I test with gtest?
+//
+// googletest/googletest/docs/primer.md
+
+#include <string.h>
+
+#include "utils.h"
+#include "gtest/gtest.h"
+
+/*
+
+int randint(int N);
+int randrange(int M, int N);
+char *repr(const char *data);
+const char *strnstr(const char *source, int len, const char *needle);
+int rstrnstr(const char *buffer, int len, const char *find);
+int string_insert(char *buffer, int max_length, int pos, const char *insert);
+void pcopy(char *pstring, char *str);
+
+*/
+
+namespace {
+
+TEST(UtilsRandRange, rangehits_10k) {
+  int x;
+  int rmin = 5, rmax = 200;
+  int max = -rmax, min = rmax * 2;
+
+  for (x = 0; x < 10000; x++) {
+    int r = randrange(rmin, rmax);
+    if (r < min)
+      min = r;
+    if (r > max)
+      max = r;
+  }
+
+  ASSERT_EQ(min, rmin);
+  ASSERT_EQ(max, rmax);
+}
+
+TEST(UtilsRandInt, rangeint_10k) {
+  int x;
+  int rmax = 200;
+  int max = -rmax, min = rmax * 2;
+
+  for (x = 0; x < 10000; x++) {
+    int r = randint(rmax);
+    if (r < min)
+      min = r;
+    if (r > max)
+      max = r;
+  }
+
+  ASSERT_EQ(min, 0);
+  ASSERT_EQ(max, rmax - 1);
+}
+
+TEST(UtilsStrNStr, strnstr) {
+  // const char *strnstr(const char *source, int len, const char *needle);
+  char buffer[20];
+  int size = sizeof(buffer);
+  memset(buffer, ' ', size);
+  ASSERT_EQ(strnstr(buffer, size, "?"), (char *)NULL);
+  buffer[10] = '?';
+  ASSERT_EQ(strnstr(buffer, size, "?"), &buffer[10]);
+  ASSERT_EQ(strnstr(buffer, 10, "?"), (char *)NULL);
+  buffer[11] = '!';
+  ASSERT_EQ(strnstr(buffer, size, "?!"), &buffer[10]);
+  ASSERT_EQ(strnstr(buffer, size, "??"), (char *)NULL);
+  // Matches first char, but the length doesn't allow for a complete match.
+  ASSERT_EQ(strnstr(buffer, 10, "?!"), (char *)NULL);
+  // There, enough chars to find a match
+  ASSERT_EQ(strnstr(buffer, 11, "?!"), &buffer[10]);
+  buffer[2] = '?';
+  buffer[5] = '?';
+  buffer[1] = '!';
+  // First char matches, but not complete
+  ASSERT_EQ(strnstr(buffer, 10, "?!"), (char *)NULL);
+  ASSERT_EQ(strnstr(buffer, 11, "?!"), &buffer[10]);
+}
+
+TEST(UtilsStringInsert, strin) {
+  // int string_insert(char *buffer, int max_length, int pos, const char
+  // *insert);
+  char buffer[20];
+  int size = sizeof(buffer);
+  memset(buffer, 1, size);
+
+  strcpy(buffer, "STARTEND");
+
+  ASSERT_EQ(string_insert(buffer, size, 5, "XX"), 1);
+  ASSERT_EQ(strncmp(buffer + 5, "XX", 2), 0);
+  ASSERT_STREQ(buffer, "STARTXXEND");
+  ASSERT_EQ(string_insert(buffer, size, 5, "ZZZ"), 1);
+  ASSERT_STREQ(buffer, "STARTZZZXXEND");
+  // 13 chars
+  ASSERT_EQ(string_insert(buffer, size, 0, "12345"), 1);
+  // 18 chars
+  ASSERT_STREQ(buffer, "12345STARTZZZXXEND");
+  ASSERT_EQ(string_insert(buffer, size, 5, "FAIL"), 0);
+  ASSERT_EQ(string_insert(buffer, size, size, "!"), 0);
+  ASSERT_EQ(string_insert(buffer, size, strlen(buffer), "!"), 1);
+  ASSERT_STREQ(buffer, "12345STARTZZZXXEND!");
+  // String is at max size.
+  ASSERT_EQ(strlen(buffer), size - 1);
+  // Can't insert base the end of the buffer
+  ASSERT_EQ(string_insert(buffer, size, strlen(buffer), "!"), 0);
+  ASSERT_STREQ(buffer, "12345STARTZZZXXEND!");
+}
+
+} // namespace

+ 15 - 1
utils.cpp

@@ -1,7 +1,7 @@
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
 
 #include "utils.h"
 
@@ -161,11 +161,25 @@ int rstrnstr(const char *buffer, int len, const char *find) {
  * string_insert()
  * Inserts a string into a given position.
  * This safely checks to make sure the buffer isn't overrun.
+ *
+ * buffer is a c null terminated string.
  */
 int string_insert(char *buffer, int max_length, int pos, const char *insert) {
+  /*
   assert(max_length > pos);
   assert(buffer != NULL);
   assert(insert != NULL);
+  */
+  if (pos >= max_length)
+    return 0;
+  if (buffer == NULL)
+    return 0;
+  if (insert == NULL)
+    return 0;
+  if (strlen(insert) == 0)
+    return 0;
+  if (pos > strlen(buffer))
+    return 0;
 
   if (strlen(buffer) + strlen(insert) >= max_length) {
     /*