我有这个代码示例,在 googletest 中定义了一个夹具。
#include "solver.h"
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class UnitTest_solver : public ::testing::Test
{
protected:
static void SetUpTestCase()
{
// setup table with data
m_col = 2;
m_test_data = new double*[m_row];
for(int i = 0 ; i < m_col ; i++)
m_test_data[i] = new double[m_row];
ifstream data_x;
data_x.open("exponential2X.txt");
// etc... : i setup table m_test_data in a few more lines
}
static void TearDownTestCase()
{
for(int i = 0 ; i < m_row ; i++)
delete[] m_test_data[i];
delete[] m_test_data;
}
static double chi_sqr(double* x, double(*func)(double*, double*, int))
{
if(ncol < 2)
return;
double fx = 0;
double * row_i = new double[ncol - 1];
for(int i = 0 ; i < nrow ; i++)
{
for(int j = 0 ; j < ncol - 1 ; j++)
row_i[j] = data[i][j];
fx += pow(data[i][0] - (*func)(x, row_i, ncol - 1), 2.0);
}
return fx;
}
static double func_1(double* x, double* dbl, int nb_param)
{
if(nb_param != 2)
return 0;
return x[0] * exp(-1 * x[1] * dbl[0]);
}
static double warpPt2apply_func(double * parameters, int ncol, void * userinfo, int nb_p)
{
return chi_sqr(parameters, (ncol - 1), (*func_1));
}
static double** m_test_data;
static int m_col, m_row;
};
TEST_F(UnitTest_solver, testFunc1)
{
Solver* solver = new Solver();
solver->set_pointer(warpPt2apply_func, (void*)NULL);
}
我有 LNK 2001 错误,可能与使用静态数据成员有关,指向成员m_test_data
, m_col
, m_row
. 是不是因为一开始就为测试套件中的每个新测试调用 SetUpTestCase() 方法,而静态成员应该一劳永逸地初始化。
任何的想法?
谢谢