GTest unit testing framework

Currently covers only small set of functionality.
This commit is contained in:
Sergey Sharybin
2014-06-18 22:49:17 +10:00
committed by Campbell Barton
parent 47ec0394ca
commit 306cbb82ec
88 changed files with 36146 additions and 286 deletions

View File

@@ -2,3 +2,6 @@
# Python CTests
add_subdirectory(python)
# GTest
add_subdirectory(gtests)

View File

@@ -0,0 +1,13 @@
# GTest
if(WITH_TESTS)
Include(GTestTesting)
# Otherwise we get warnings here that we cant fix in external projects
remove_strict_flags()
add_subdirectory(testing)
add_subdirectory(blenlib)
endif()

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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}")

View File

@@ -0,0 +1,48 @@
# ***** 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
.
../
../../../extern/libmv/third_party/gflags
../../../extern/gtest/include
)
if(WIN32)
list(APPEND INC
../../../extern/libmv/third_party/glog/src/windows
)
else()
list(APPEND INC
../../../extern/libmv/third_party/glog/src
)
endif()
set(INC_SYS
)
set(SRC
testing_main.cc
)
blender_add_lib(bf_testing_main "${SRC}" "${INC}" "${INC_SYS}")

View File

@@ -0,0 +1,78 @@
#ifndef __BLENDER_TESTING_H__
#define __BLENDER_TESTING_H__
#include "glog/logging.h"
#include "gflags/gflags.h"
#include "gtest/gtest.h"
#define EXPECT_V3_NEAR(a, b, eps) \
{ \
EXPECT_NEAR(a[0], b[0], eps); \
EXPECT_NEAR(a[1], b[1], eps); \
EXPECT_NEAR(a[2], b[2], eps); \
} (void) 0
#define EXPECT_MATRIX_NEAR(a, b, tolerance) \
do { \
bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
if (dims_match) { \
for (int r = 0; r < a.rows(); ++r) { \
for (int c = 0; c < a.cols(); ++c) { \
EXPECT_NEAR(a(r, c), b(r, c), tolerance) \
<< "r=" << r << ", c=" << c << "."; \
} \
} \
} \
} while(false);
#define EXPECT_MATRIX_NEAR_ZERO(a, tolerance) \
do { \
for (int r = 0; r < a.rows(); ++r) { \
for (int c = 0; c < a.cols(); ++c) { \
EXPECT_NEAR(0.0, a(r, c), tolerance) \
<< "r=" << r << ", c=" << c << "."; \
} \
} \
} while(false);
#define EXPECT_MATRIX_EQ(a, b) \
do { \
bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
if (dims_match) { \
for (int r = 0; r < a.rows(); ++r) { \
for (int c = 0; c < a.cols(); ++c) { \
EXPECT_EQ(a(r, c), b(r, c)) \
<< "r=" << r << ", c=" << c << "."; \
} \
} \
} \
} while(false);
// Check that sin(angle(a, b)) < tolerance.
#define EXPECT_MATRIX_PROP(a, b, tolerance) \
do { \
bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
if (dims_match) { \
double c = CosinusBetweenMatrices(a, b); \
if (c * c < 1) { \
double s = sqrt(1 - c * c); \
EXPECT_NEAR(0, s, tolerance); \
} \
} \
} while(false);
#ifdef LIBMV_NUMERIC_NUMERIC_H
template<class TMat>
double CosinusBetweenMatrices(const TMat &a, const TMat &b) {
return (a.array() * b.array()).sum() /
libmv::FrobeniusNorm(a) / libmv::FrobeniusNorm(b);
}
#endif
#endif // __BLENDER_TESTING_H__

View File

@@ -0,0 +1,36 @@
/*
* ***** 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): Blender Foundation,
* Sergey Sharybin
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "testing/testing.h"
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
return RUN_ALL_TESTS();
}