Cycles: Add some utility tests using GTests
authorSergey Sharybin <sergey.vfx@gmail.com>
Sat, 6 Feb 2016 18:09:44 +0000 (19:09 +0100)
committerSergey Sharybin <sergey.vfx@gmail.com>
Sat, 6 Feb 2016 18:19:20 +0000 (19:19 +0100)
This is an initial move to have unittests to at least cover
utility functions, which then could be extended further to
test such areas as shader optimization and such.

Currently only based on initial "infrastructure" layout and
writing tests needed to test the no-boost patch.

Note: This patch starts to use "<dir>/<header>.h" notation
for the include statements which i just got used to do in
other projects. Something what would be cool to use globally
in the code eventually.

Reviewers: dingto, juicyfruit, lukasstockner97, brecht

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D1770

intern/cycles/CMakeLists.txt
intern/cycles/test/CMakeLists.txt [new file with mode: 0644]
intern/cycles/test/util_aligned_malloc_test.cpp [new file with mode: 0644]
intern/cycles/test/util_path_test.cpp [new file with mode: 0644]
intern/cycles/test/util_string_test.cpp [new file with mode: 0644]

index bd75f4d..2a08945 100644 (file)
@@ -225,6 +225,11 @@ add_subdirectory(render)
 add_subdirectory(subd)
 add_subdirectory(util)
 
+# TODO(sergey): Make this to work with standalone repository.
+if(WITH_GTESTS)
+       add_subdirectory(test)
+endif()
+
 if(NOT WITH_BLENDER AND WITH_CYCLES_STANDALONE)
        delayed_do_install(${CMAKE_BINARY_DIR}/bin)
 endif()
diff --git a/intern/cycles/test/CMakeLists.txt b/intern/cycles/test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..f8bd58c
--- /dev/null
@@ -0,0 +1,30 @@
+if(WITH_GTESTS)
+       Include(GTestTesting)
+
+       # Otherwise we get warnings here that we cant fix in external projects
+       remove_strict_flags()
+endif()
+
+macro(CYCLES_TEST SRC EXTRA_LIBS)
+       if(WITH_GTESTS)
+               BLENDER_SRC_GTEST("cycles_${SRC}" "${SRC}_test.cpp" "${EXTRA_LIBS}")
+       endif()
+endmacro()
+
+set(INC
+       .
+       ..
+       ../util
+)
+
+include_directories(${INC})
+
+link_directories(${BOOST_LIBPATH})
+link_directories(${OPENIMAGEIO_LIBPATH})
+
+set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINKFLAGS}")
+set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LINKFLAGS_DEBUG}")
+
+CYCLES_TEST(util_aligned_malloc "cycles_util")
+CYCLES_TEST(util_path "cycles_util;${BOOST_LIBRARIES};${OPENIMAGEIO_LIBRARIES}")
+CYCLES_TEST(util_string "cycles_util;${BOOST_LIBRARIES}")
diff --git a/intern/cycles/test/util_aligned_malloc_test.cpp b/intern/cycles/test/util_aligned_malloc_test.cpp
new file mode 100644 (file)
index 0000000..479070f
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2011-2016 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "testing/testing.h"
+
+#include "util/util_aligned_malloc.h"
+
+#define CHECK_ALIGNMENT(ptr, align) EXPECT_EQ(0, (size_t)ptr % align)
+
+CCL_NAMESPACE_BEGIN
+
+TEST(util_aligned_malloc, aligned_malloc_16)
+{
+       int *mem = (int*)util_aligned_malloc(sizeof(int), 16);
+       CHECK_ALIGNMENT(mem, 16);
+       util_aligned_free(mem);
+}
+
+/* On Apple we currently only support 16 bytes alignment. */
+#ifndef __APPLE__
+TEST(util_aligned_malloc, aligned_malloc_32)
+{
+       int *mem = (int*)util_aligned_malloc(sizeof(int), 32);
+       CHECK_ALIGNMENT(mem, 32);
+       util_aligned_free(mem);
+}
+#endif  /* __APPLE__ */
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/test/util_path_test.cpp b/intern/cycles/test/util_path_test.cpp
new file mode 100644 (file)
index 0000000..92be1dd
--- /dev/null
@@ -0,0 +1,401 @@
+/*
+ * Copyright 2011-2016 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "testing/testing.h"
+
+#include "util/util_path.h"
+
+CCL_NAMESPACE_BEGIN
+
+/* ******** Tests for path_filename() ******** */
+
+#ifndef _WIN32
+TEST(util_path_filename, simple_unix)
+{
+       string str = path_filename("/tmp/foo.txt");
+       EXPECT_EQ("foo.txt", str);
+}
+
+TEST(util_path_filename, root_unix)
+{
+       string str = path_filename("/");
+       EXPECT_EQ("/", str);
+}
+
+TEST(util_path_filename, last_slash_unix)
+{
+       string str = path_filename("/tmp/foo.txt/");
+       EXPECT_EQ(".", str);
+}
+
+TEST(util_path_filename, alternate_slash_unix)
+{
+       string str = path_filename("/tmp\\foo.txt");
+       EXPECT_EQ("tmp\\foo.txt", str);
+}
+#endif  /* !_WIN32 */
+
+TEST(util_path_filename, file_only)
+{
+       string str = path_filename("foo.txt");
+       EXPECT_EQ("foo.txt", str);
+}
+
+TEST(util_path_filename, empty)
+{
+       string str = path_filename("");
+       EXPECT_EQ("", str);
+}
+
+#ifdef _WIN32
+TEST(util_path_filename, simple_windows)
+{
+       string str = path_filename("C:\\tmp\\foo.txt");
+       EXPECT_EQ("foo.txt", str);
+}
+
+TEST(util_path_filename, root_windows)
+{
+       string str = path_filename("C:\\");
+       EXPECT_EQ("\\", str);
+}
+
+TEST(util_path_filename, last_slash_windows)
+{
+       string str = path_filename("C:\\tmp\\foo.txt\\");
+       EXPECT_EQ(".", str);
+}
+
+TEST(util_path_filename, alternate_slash_windows)
+{
+       string str = path_filename("C:\\tmp/foo.txt");
+       EXPECT_EQ("foo.txt", str);
+}
+#endif  /* _WIN32 */
+
+/* ******** Tests for path_dirname() ******** */
+
+#ifndef _WIN32
+TEST(util_path_dirname, simple_unix)
+{
+       string str = path_dirname("/tmp/foo.txt");
+       EXPECT_EQ("/tmp", str);
+}
+
+TEST(util_path_dirname, root_unix)
+{
+       string str = path_dirname("/");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_path_dirname, last_slash_unix)
+{
+       string str = path_dirname("/tmp/foo.txt/");
+       EXPECT_EQ("/tmp/foo.txt", str);
+}
+
+TEST(util_path_dirname, alternate_slash_unix)
+{
+       string str = path_dirname("/tmp\\foo.txt");
+       EXPECT_EQ("/", str);
+}
+#endif  /* !_WIN32 */
+
+TEST(util_path_dirname, file_only)
+{
+       string str = path_dirname("foo.txt");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_path_dirname, empty)
+{
+       string str = path_dirname("");
+       EXPECT_EQ("", str);
+}
+
+#ifdef _WIN32
+TEST(util_path_dirname, simple_windows)
+{
+       string str = path_dirname("C:\\tmp\\foo.txt");
+       EXPECT_EQ("C:\\tmp", str);
+}
+
+TEST(util_path_dirname, root_windows)
+{
+       string str = path_dirname("C:\\");
+       EXPECT_EQ("C:", str);
+}
+
+TEST(util_path_dirname, last_slash_windows)
+{
+       string str = path_dirname("C:\\tmp\\foo.txt\\");
+       EXPECT_EQ("C:\\tmp\\foo.txt", str);
+}
+
+TEST(util_path_dirname, alternate_slash_windows)
+{
+       string str = path_dirname("C:\\tmp/foo.txt");
+       EXPECT_EQ("C:\\tmp", str);
+}
+#endif  /* _WIN32 */
+
+/* ******** Tests for path_join() ******** */
+
+TEST(util_path_join, empty_both)
+{
+       string str = path_join("", "");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_path_join, empty_directory)
+{
+       string str = path_join("", "foo.txt");
+       EXPECT_EQ("foo.txt", str);
+}
+
+TEST(util_path_join, empty_filename)
+{
+       string str = path_join("foo", "");
+       EXPECT_EQ("foo", str);
+}
+
+#ifndef _WIN32
+TEST(util_path_join, simple_unix)
+{
+       string str = path_join("foo", "bar");
+       EXPECT_EQ("foo/bar", str);
+}
+
+TEST(util_path_join, directory_slash_unix)
+{
+       string str = path_join("foo/", "bar");
+       EXPECT_EQ("foo/bar", str);
+}
+
+TEST(util_path_join, filename_slash_unix)
+{
+       string str = path_join("foo", "/bar");
+       EXPECT_EQ("foo/bar", str);
+}
+
+TEST(util_path_join, both_slash_unix)
+{
+       string str = path_join("foo/", "/bar");
+       EXPECT_EQ("foo//bar", str);
+}
+
+TEST(util_path_join, directory_alternate_slash_unix)
+{
+       string str = path_join("foo\\", "bar");
+       EXPECT_EQ("foo\\/bar", str);
+}
+
+TEST(util_path_join, filename_alternate_slash_unix)
+{
+       string str = path_join("foo", "\\bar");
+       EXPECT_EQ("foo/\\bar", str);
+}
+
+TEST(util_path_join, both_alternate_slash_unix)
+{
+       string str = path_join("foo", "\\bar");
+       EXPECT_EQ("foo/\\bar", str);
+}
+
+TEST(util_path_join, empty_dir_filename_slash_unix)
+{
+       string str = path_join("", "/foo.txt");
+       EXPECT_EQ("/foo.txt", str);
+}
+
+TEST(util_path_join, empty_dir_filename_alternate_slash_unix)
+{
+       string str = path_join("", "\\foo.txt");
+       EXPECT_EQ("\\foo.txt", str);
+}
+
+TEST(util_path_join, empty_filename_dir_slash_unix)
+{
+       string str = path_join("foo/", "");
+       EXPECT_EQ("foo/", str);
+}
+
+TEST(util_path_join, empty_filename_dir_alternate_slash_unix)
+{
+       string str = path_join("foo\\", "");
+       EXPECT_EQ("foo\\", str);
+}
+#else  /* !_WIN32 */
+TEST(util_path_join, simple_windows)
+{
+       string str = path_join("foo", "bar");
+       EXPECT_EQ("foo\\bar", str);
+}
+
+TEST(util_path_join, directory_slash_windows)
+{
+       string str = path_join("foo\\", "bar");
+       EXPECT_EQ("foo\\bar", str);
+}
+
+TEST(util_path_join, filename_slash_windows)
+{
+       string str = path_join("foo", "\\bar");
+       EXPECT_EQ("foo\\bar", str);
+}
+
+TEST(util_path_join, both_slash_windows)
+{
+       string str = path_join("foo\\", "\\bar");
+       EXPECT_EQ("foo\\\\bar", str);
+}
+
+TEST(util_path_join, directory_alternate_slash_windows)
+{
+       string str = path_join("foo/", "bar");
+       EXPECT_EQ("foo/bar", str);
+}
+
+TEST(util_path_join, filename_alternate_slash_windows)
+{
+       string str = path_join("foo", "/bar");
+       EXPECT_EQ("foo/bar", str);
+}
+
+TEST(util_path_join, both_alternate_slash_windows)
+{
+       string str = path_join("foo/", "/bar");
+       EXPECT_EQ("foo//bar", str);
+}
+
+TEST(util_path_join, empty_dir_filename_slash_windows)
+{
+       string str = path_join("", "\\foo.txt");
+       EXPECT_EQ("\\foo.txt", str);
+}
+
+TEST(util_path_join, empty_dir_filename_alternate_slash_windows)
+{
+       string str = path_join("", "/foo.txt");
+       EXPECT_EQ("/foo.txt", str);
+}
+
+TEST(util_path_join, empty_filename_dir_slash_windows)
+{
+       string str = path_join("foo\\", "");
+       EXPECT_EQ("foo\\", str);
+}
+
+TEST(util_path_join, empty_filename_dir_alternate_slash_windows)
+{
+       string str = path_join("foo/", "");
+       EXPECT_EQ("foo/", str);
+}
+#endif  /* !_WIN32 */
+
+/* ******** Tests for path_escape() ******** */
+
+TEST(util_path_escape, no_escape_chars)
+{
+       string str = path_escape("/tmp/foo/bar");
+       EXPECT_EQ("/tmp/foo/bar", str);
+}
+
+TEST(util_path_escape, simple)
+{
+       string str = path_escape("/tmp/foo bar");
+       EXPECT_EQ("/tmp/foo\\ bar", str);
+}
+
+TEST(util_path_escape, simple_end)
+{
+       string str = path_escape("/tmp/foo/bar ");
+       EXPECT_EQ("/tmp/foo/bar\\ ", str);
+}
+
+TEST(util_path_escape, multiple)
+{
+       string str = path_escape("/tmp/foo  bar");
+       EXPECT_EQ("/tmp/foo\\ \\ bar", str);
+}
+
+TEST(util_path_escape, simple_multiple_end)
+{
+       string str = path_escape("/tmp/foo/bar  ");
+       EXPECT_EQ("/tmp/foo/bar\\ \\ ", str);
+}
+
+/* ******** Tests for path_is_relative() ******** */
+
+TEST(util_path_is_relative, filename)
+{
+       bool is_relative = path_is_relative("foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+
+#ifndef _WIN32
+TEST(util_path_is_relative, absolute_unix)
+{
+       bool is_relative = path_is_relative("/tmp/foo.txt");
+       EXPECT_FALSE(is_relative);
+}
+
+TEST(util_path_is_relative, relative_dir_unix)
+{
+       bool is_relative = path_is_relative("tmp/foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+
+TEST(util_path_is_relative, absolute_windir_on_unix)
+{
+       bool is_relative = path_is_relative("C:\\tmp\\foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+
+TEST(util_path_is_relative, relative_windir_on_unix)
+{
+       bool is_relative = path_is_relative("tmp\\foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+#endif /* !_WIN32 */
+
+#ifdef _WIN32
+TEST(util_path_is_relative, absolute_windows)
+{
+       bool is_relative = path_is_relative("C:\\tmp\\foo.txt");
+       EXPECT_FALSE(is_relative);
+}
+
+TEST(util_path_is_relative, relative_dir_windows)
+{
+       bool is_relative = path_is_relative("tmp\\foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+
+TEST(util_path_is_relative, absolute_unixdir_on_windows)
+{
+       bool is_relative = path_is_relative("/tmp/foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+
+TEST(util_path_is_relative, relative_unixdir_on_windows)
+{
+       bool is_relative = path_is_relative("tmp/foo.txt");
+       EXPECT_TRUE(is_relative);
+}
+#endif /* _WIN32 */
+
+CCL_NAMESPACE_END
diff --git a/intern/cycles/test/util_string_test.cpp b/intern/cycles/test/util_string_test.cpp
new file mode 100644 (file)
index 0000000..e502a35
--- /dev/null
@@ -0,0 +1,248 @@
+/*
+ * Copyright 2011-2016 Blender Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "testing/testing.h"
+
+#include "util/util_string.h"
+
+CCL_NAMESPACE_BEGIN
+
+/* ******** Tests for string_printf() ******** */
+
+TEST(util_string_printf, no_format)
+{
+       string str = string_printf("foo bar");
+       EXPECT_EQ(str, "foo bar");
+}
+
+TEST(util_string_printf, int_number)
+{
+       string str = string_printf("foo %d bar", 314);
+       EXPECT_EQ(str, "foo 314 bar");
+}
+
+TEST(util_string_printf, float_number_default_precision)
+{
+       string str = string_printf("foo %f bar", 3.1415);
+       EXPECT_EQ(str, "foo 3.141500 bar");
+}
+
+TEST(util_string_printf, float_number_custom_precision)
+{
+       string str = string_printf("foo %.1f bar", 3.1415);
+       EXPECT_EQ(str, "foo 3.1 bar");
+}
+
+/* ******** Tests for string_printf() ******** */
+
+TEST(util_string_iequals, empty_a)
+{
+       bool equals = string_iequals("", "foo");
+       EXPECT_FALSE(equals);
+}
+
+TEST(util_string_iequals, empty_b)
+{
+       bool equals = string_iequals("foo", "");
+       EXPECT_FALSE(equals);
+}
+
+TEST(util_string_iequals, same_register)
+{
+       bool equals = string_iequals("foo", "foo");
+       EXPECT_TRUE(equals);
+}
+
+TEST(util_string_iequals, different_register)
+{
+       bool equals = string_iequals("XFoo", "XfoO");
+       EXPECT_TRUE(equals);
+}
+
+/* ******** Tests for string_split() ******** */
+
+TEST(util_string_split, empty)
+{
+       vector<string> tokens;
+       string_split(tokens, "");
+       EXPECT_EQ(0, tokens.size());
+}
+
+TEST(util_string_split, only_spaces)
+{
+       vector<string> tokens;
+       string_split(tokens, "   \t\t \t");
+       EXPECT_EQ(0, tokens.size());
+}
+
+TEST(util_string_split, single)
+{
+       vector<string> tokens;
+       string_split(tokens, "foo");
+       EXPECT_EQ(1, tokens.size());
+       EXPECT_EQ("foo", tokens[0]);
+}
+
+TEST(util_string_split, simple)
+{
+       vector<string> tokens;
+       string_split(tokens, "foo a bar b");
+       EXPECT_EQ(4, tokens.size());
+       EXPECT_EQ("foo", tokens[0]);
+       EXPECT_EQ("a", tokens[1]);
+       EXPECT_EQ("bar", tokens[2]);
+       EXPECT_EQ("b", tokens[3]);
+}
+
+TEST(util_string_split, multiple_spaces)
+{
+       vector<string> tokens;
+       string_split(tokens, " \t foo \ta bar b\t  ");
+       EXPECT_EQ(4, tokens.size());
+       EXPECT_EQ("foo", tokens[0]);
+       EXPECT_EQ("a", tokens[1]);
+       EXPECT_EQ("bar", tokens[2]);
+       EXPECT_EQ("b", tokens[3]);
+}
+
+/* ******** Tests for string_replace() ******** */
+
+TEST(util_string_replace, empty_haystack_and_other)
+{
+       string str = "";
+       string_replace(str, "x", "");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_string_replace, empty_haystack)
+{
+       string str = "";
+       string_replace(str, "x", "y");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_string_replace, empty_other)
+{
+       string str = "x";
+       string_replace(str, "x", "");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_string_replace, long_haystack_empty_other)
+{
+       string str = "a x b xxc";
+       string_replace(str, "x", "");
+       EXPECT_EQ("a  b c", str);
+}
+
+TEST(util_string_replace, long_haystack)
+{
+       string str = "a x b xxc";
+       string_replace(str, "x", "FOO");
+       EXPECT_EQ("a FOO b FOOFOOc", str);
+}
+
+/* ******** Tests for string_endswith() ******** */
+
+TEST(util_string_endswith, empty_both)
+{
+       bool endswith = string_endswith("", "");
+       EXPECT_TRUE(endswith);
+}
+
+TEST(util_string_endswith, empty_string)
+{
+       bool endswith = string_endswith("", "foo");
+       EXPECT_FALSE(endswith);
+}
+
+TEST(util_string_endswith, empty_end)
+{
+       bool endswith = string_endswith("foo", "");
+       EXPECT_TRUE(endswith);
+}
+
+TEST(util_string_endswith, simple_true)
+{
+       bool endswith = string_endswith("foo bar", "bar");
+       EXPECT_TRUE(endswith);
+}
+
+TEST(util_string_endswith, simple_false)
+{
+       bool endswith = string_endswith("foo bar", "foo");
+       EXPECT_FALSE(endswith);
+}
+
+/* ******** Tests for string_strip() ******** */
+
+TEST(util_string_strip, empty)
+{
+       string str = string_strip("");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_string_strip, only_spaces)
+{
+       string str = string_strip("      ");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_string_strip, no_spaces)
+{
+       string str = string_strip("foo bar");
+       EXPECT_EQ("foo bar", str);
+}
+
+TEST(util_string_strip, with_spaces)
+{
+       string str = string_strip("    foo bar ");
+       EXPECT_EQ("foo bar", str);
+}
+
+/* ******** Tests for string_remove_trademark() ******** */
+
+TEST(util_string_remove_trademark, empty)
+{
+       string str = string_remove_trademark("");
+       EXPECT_EQ("", str);
+}
+
+TEST(util_string_remove_trademark, no_trademark)
+{
+       string str = string_remove_trademark("foo bar");
+       EXPECT_EQ("foo bar", str);
+}
+
+TEST(util_string_remove_trademark, only_tm)
+{
+       string str = string_remove_trademark("foo bar(TM) zzz");
+       EXPECT_EQ("foo bar zzz", str);
+}
+
+TEST(util_string_remove_trademark, only_r)
+{
+       string str = string_remove_trademark("foo bar(R) zzz");
+       EXPECT_EQ("foo bar zzz", str);
+}
+
+TEST(util_string_remove_trademark, both)
+{
+       string str = string_remove_trademark("foo bar(TM)(R) zzz");
+       EXPECT_EQ("foo bar zzz", str);
+}
+
+CCL_NAMESPACE_END