diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2014-06-18 16:49:17 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-06-18 20:09:16 +0400 |
commit | 306cbb82ecf0d7c1ba4fb0a1240175b1976bd25b (patch) | |
tree | e9eac65bf57126ac233f22fe88cc00df137ec4e5 /tests/gtests/blenlib | |
parent | 47ec0394ca3d03e07c07a67e8f8d1625aedd39dd (diff) |
GTest unit testing framework
Currently covers only small set of functionality.
Diffstat (limited to 'tests/gtests/blenlib')
-rw-r--r-- | tests/gtests/blenlib/BLI_math_color_test.cc | 88 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_math_geom_test.cc | 21 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_path_util_test.cc | 219 | ||||
-rw-r--r-- | tests/gtests/blenlib/CMakeLists.txt | 39 |
4 files changed, 367 insertions, 0 deletions
diff --git a/tests/gtests/blenlib/BLI_math_color_test.cc b/tests/gtests/blenlib/BLI_math_color_test.cc new file mode 100644 index 00000000000..dd3b3f1add1 --- /dev/null +++ b/tests/gtests/blenlib/BLI_math_color_test.cc @@ -0,0 +1,88 @@ +#include "testing/testing.h" + +#include "BLI_math.h" + +TEST(mathutils, RGBToHSVRoundtrip) +{ + float orig_rgb[3] = {0.1f, 0.2f, 0.3f}; + float hsv[3], rgb[3]; + rgb_to_hsv_v(orig_rgb, hsv); + hsv_to_rgb_v(hsv, rgb); + EXPECT_V3_NEAR(orig_rgb, rgb, 1e-5); +} + +TEST(mathutils, RGBToHSLRoundtrip) +{ + float orig_rgb[3] = {0.1f, 0.2f, 0.3f}; + float hsl[3], rgb[3]; + rgb_to_hsl_v(orig_rgb, hsl); + hsl_to_rgb_v(hsl, rgb); + EXPECT_V3_NEAR(orig_rgb, rgb, 1e-5); +} + +TEST(mathutils, RGBToYUVRoundtrip) +{ + float orig_rgb[3] = {0.1f, 0.2f, 0.3f}; + float yuv[3], rgb[3]; + rgb_to_yuv(orig_rgb[0], orig_rgb[1], orig_rgb[2], + &yuv[0], &yuv[1], &yuv[2]); + yuv_to_rgb(yuv[0], yuv[1], yuv[2], + &rgb[0], &rgb[1], &rgb[2]); + EXPECT_V3_NEAR(orig_rgb, rgb, 1e-4); +} + +TEST(mathutils, RGBToYCCRoundtrip) +{ + float orig_rgb[3] = {0.1f, 0.2f, 0.3f}; + float ycc[3], rgb[3]; + + rgb_to_ycc(orig_rgb[0], orig_rgb[1], orig_rgb[2], + &ycc[0], &ycc[1], &ycc[2], + BLI_YCC_ITU_BT601); + ycc_to_rgb(ycc[0], ycc[1], ycc[2], + &rgb[0], &rgb[1], &rgb[2], + BLI_YCC_ITU_BT601); + EXPECT_V3_NEAR(orig_rgb, rgb, 1e-3); + + rgb_to_ycc(orig_rgb[0], orig_rgb[1], orig_rgb[2], + &ycc[0], &ycc[1], &ycc[2], + BLI_YCC_ITU_BT709); + ycc_to_rgb(ycc[0], ycc[1], ycc[2], + &rgb[0], &rgb[1], &rgb[2], + BLI_YCC_ITU_BT709); + EXPECT_V3_NEAR(orig_rgb, rgb, 1e-3); + + rgb_to_ycc(orig_rgb[0], orig_rgb[1], orig_rgb[2], + &ycc[0], &ycc[1], &ycc[2], + BLI_YCC_JFIF_0_255); + ycc_to_rgb(ycc[0], ycc[1], ycc[2], + &rgb[0], &rgb[1], &rgb[2], + BLI_YCC_JFIF_0_255); + EXPECT_V3_NEAR(orig_rgb, rgb, 1e-3); +} + +TEST(mathutils, LinearRGBTosRGBNearZero) +{ + float linear_color = 0.002f; + float srgb_color = linearrgb_to_srgb(linear_color); + EXPECT_NEAR(0.02584f, srgb_color, 1e-5); +} + +TEST(mathutils, LinearRGBTosRGB) +{ + float linear_color = 0.75f; + float srgb_color = linearrgb_to_srgb(linear_color); + EXPECT_NEAR(0.880824f, srgb_color, 1e-5); +} + +TEST(mathutils, LinearRGBTosRGBRoundtrip) +{ + const int N = 50; + int i; + for (i = 0; i < N; ++i) { + float orig_linear_color = (float) i / N; + float srgb_color = linearrgb_to_srgb(orig_linear_color); + float linear_color = srgb_to_linearrgb(srgb_color); + EXPECT_NEAR(orig_linear_color, linear_color, 1e-5); + } +} diff --git a/tests/gtests/blenlib/BLI_math_geom_test.cc b/tests/gtests/blenlib/BLI_math_geom_test.cc new file mode 100644 index 00000000000..36784f50dd1 --- /dev/null +++ b/tests/gtests/blenlib/BLI_math_geom_test.cc @@ -0,0 +1,21 @@ +#include "testing/testing.h" + +#include "BLI_math.h" + +TEST(mathutils, DistToLine2DSimple) +{ + float p[2] = {5.0f, 1.0f}, + a[2] = {0.0f, 0.0f}, + b[2] = {2.0f, 0.0f}; + float distance = dist_to_line_v2(p, a, b); + EXPECT_NEAR(1.0f, distance, 1e-6); +} + +TEST(mathutils, DistToLineSegment2DSimple) +{ + float p[2] = {3.0f, 1.0f}, + a[2] = {0.0f, 0.0f}, + b[2] = {2.0f, 0.0f}; + float distance = dist_to_line_segment_v2(p, a, b); + EXPECT_NEAR(sqrtf(2.0f), distance, 1e-6); +} diff --git a/tests/gtests/blenlib/BLI_path_util_test.cc b/tests/gtests/blenlib/BLI_path_util_test.cc new file mode 100644 index 00000000000..20ca3731b8f --- /dev/null +++ b/tests/gtests/blenlib/BLI_path_util_test.cc @@ -0,0 +1,219 @@ +#include "testing/testing.h" + +extern "C" { +#include "BLI_fileops.h" +#include "BLI_path_util.h" +#include "../../../source/blender/imbuf/IMB_imbuf.h" +} + +/* -------------------------------------------------------------------- */ +/* stubs */ + +extern "C" { + +const char *GHOST_getUserDir(int version, const char *versionstr); +const char *GHOST_getSystemDir(int version, const char *versionstr); +#ifdef __linux__ +char *zLhm65070058860608_br_find_exe(const char *default_exe); +#endif + +const char *GHOST_getUserDir(int version, const char *versionstr) +{ + return "/home/user"; +} + +const char *GHOST_getSystemDir(int version, const char *versionstr) +{ + return "/system/path"; +} + +struct ImBuf; +void IMB_freeImBuf(struct ImBuf *ibuf) {} + +#ifdef __linux__ +char *zLhm65070058860608_br_find_exe(const char *default_exe) +{ + return NULL; +} +#endif + +} + + +/* -------------------------------------------------------------------- */ +/* tests */ + +/* BLI_cleanup_path */ +TEST(pathutils, PathUtilClean) +{ + /* "/./" -> "/" */ + { + char path[FILE_MAX] = "/a/./b/./c/./"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("/a/b/c/", path); + } + + { + char path[FILE_MAX] = "/./././"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("/", path); + } + + { + char path[FILE_MAX] = "/a/./././b/"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("/a/b/", path); + } + + /* "//" -> "/" */ + { + char path[FILE_MAX] = "a////"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("a/", path); + } + + if (0) /* FIXME */ + { + char path[FILE_MAX] = "./a////"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("./a/", path); + } + + /* "foo/bar/../" -> "foo/" */ + { + char path[FILE_MAX] = "/a/b/c/../../../"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("/", path); + } + + { + char path[FILE_MAX] = "/a/../a/b/../b/c/../c/"; + BLI_cleanup_path(NULL, path); + EXPECT_STREQ("/a/b/c/", path); + } + + { + char path[FILE_MAX] = "//../"; + BLI_cleanup_path("/a/b/c/", path); + EXPECT_STREQ("/a/b/", path); + } +} + +/* BLI_path_frame */ +TEST(pathutils, PathUtilFrame) +{ + bool ret; + + { + char path[FILE_MAX] = ""; + ret = BLI_path_frame(path, 123, 1); + EXPECT_EQ(1, ret); + EXPECT_STREQ("123", path); + } + + { + char path[FILE_MAX] = ""; + ret = BLI_path_frame(path, 123, 12); + EXPECT_EQ(1, ret); + EXPECT_STREQ("000000000123", path); + } + + { + char path[FILE_MAX] = "test_"; + ret = BLI_path_frame(path, 123, 1); + EXPECT_EQ(1, ret); + EXPECT_STREQ("test_123", path); + } + + { + char path[FILE_MAX] = "test_"; + ret = BLI_path_frame(path, 1, 12); + EXPECT_EQ(1, ret); + EXPECT_STREQ("test_000000000001", path); + } + + { + char path[FILE_MAX] = "test_############"; + ret = BLI_path_frame(path, 1, 0); + EXPECT_EQ(1, ret); + EXPECT_STREQ("test_000000000001", path); + } + + { + char path[FILE_MAX] = "test_#_#_middle"; + ret = BLI_path_frame(path, 123, 0); + EXPECT_EQ(1, ret); + EXPECT_STREQ("test_#_123_middle", path); + } + + /* intentionally fail */ + { + char path[FILE_MAX] = ""; + ret = BLI_path_frame(path, 123, 0); + EXPECT_EQ(0, ret); + EXPECT_STREQ("", path); + } + + { + char path[FILE_MAX] = "test_middle"; + ret = BLI_path_frame(path, 123, 0); + EXPECT_EQ(0, ret); + EXPECT_STREQ("test_middle", path); + } +} + +/* BLI_split_dirfile */ +TEST(pathutils, PathUtilSplitDirfile) +{ + { + const char *path = ""; + char dir[FILE_MAX], file[FILE_MAX]; + BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file)); + EXPECT_STREQ("", dir); + EXPECT_STREQ("", file); + } + + { + const char *path = "/"; + char dir[FILE_MAX], file[FILE_MAX]; + BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file)); + EXPECT_STREQ("/", dir); + EXPECT_STREQ("", file); + } + + { + const char *path = "fileonly"; + char dir[FILE_MAX], file[FILE_MAX]; + BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file)); + EXPECT_STREQ("", dir); + EXPECT_STREQ("fileonly", file); + } + + { + const char *path = "dironly/"; + char dir[FILE_MAX], file[FILE_MAX]; + BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file)); + EXPECT_STREQ("dironly/", dir); + EXPECT_STREQ("", file); + } + + { + const char *path = "/a/b"; + char dir[FILE_MAX], file[FILE_MAX]; + BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file)); + EXPECT_STREQ("/a/", dir); + EXPECT_STREQ("b", file); + } + + { + const char *path = "/dirtoobig/filetoobig"; + char dir[5], file[5]; + BLI_split_dirfile(path, dir, file, sizeof(dir), sizeof(file)); + EXPECT_STREQ("/dir", dir); + EXPECT_STREQ("file", file); + + BLI_split_dirfile(path, dir, file, 1, 1); + EXPECT_STREQ("", dir); + EXPECT_STREQ("", file); + } +} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt new file mode 100644 index 00000000000..4980ca2e8ad --- /dev/null +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -0,0 +1,39 @@ +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# The Original Code is Copyright (C) 2014, Blender Foundation +# All rights reserved. +# +# Contributor(s): Sergey Sharybin +# +# ***** END GPL LICENSE BLOCK ***** + +set(INC + . + ../ + ../../../source/blender/blenlib + ../../../intern/guardedalloc +) + +include_directories(${INC}) + +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}") + + +BLENDER_TEST(BLI_math_color "bf_blenlib") +BLENDER_TEST(BLI_math_geom "bf_blenlib") +BLENDER_TEST(BLI_path_util "bf_blenlib;extern_wcwidth;${ZLIB_LIBRARIES}") |