2013-08-02 2 views
0

Я пытаюсь использовать gtest, но у меня возникают трудности. Скажите, пожалуйста, что мне делать, чтобы запустить все тесты. код, который я хотел бы проверить (только заголовок файла, реализация пренебрежимо)Gtest: ошибка компиляции

ifndef PedestalSubstractorModel_h 
#define PedestalSubstractorModel_h 

#include <TROOT.h> 
#include <TChain.h> 
#include <TFile.h> 
#include<TH2D.h> 

// Header file for the classes stored in the TTree if any. 
#include <vector> 

class PedestalSubstractorModel { 
public : 
    TTree   *fChain; //!pointer to the analyzed TTree or TChain 
    Int_t   fCurrent; //!current Tree number in a TChain 

    // Declaration of leaf types 
    std::vector<int>  *vec; 
    std::vector<int>  pedestal; 
    std::vector<int>  afterPedestal; 
    std::vector<int>  commonMode; 
    TH2D *histo; 

    // List of branches 
    TBranch  *b_vec; //! 

    PedestalSubstractorModel(TTree *tree=0); 
    virtual ~PedestalSubstractorModel(); 
    virtual Int_t GetEntry(Long64_t entry); 
    virtual Long64_t LoadTree(Long64_t entry); 
    virtual void  Init(TTree *tree); 
    virtual void  ShowRAW(); 
    virtual void  ComputeParameters(); 
    virtual void  SubstractPedestal(); 
    virtual void  CommonMode(); 
    inline TH2D* GetHisto(){return histo;} 
}; 

#endif 

GTEST файл ModuleTest.cc

#include "../PedestalSubstractorModel.h" 
#include "gtest/gtest.h" 
#include <cmath> 


namespace { 


class ModuleTest : public ::testing::Test { 

protected: 
PedestalSubstractorModel *p; 

ModuleTest() { 

    } 

    virtual ~ModuleTest() { 
    // You can do clean-up work that doesn't throw exceptions here. 
    } 
    virtual void SetUp() { 
    p=new PedestalSubstractorModel(); 
    } 


}; 

// Tests that the Foo::Bar() method does Abc. 
TEST_F(PedestalTest,ShowRAWTest) { 
    p->ShowRAW(); 
    EXPECT_EQ(p->GetHisto()->GetEntries(),5000); 
} 

// Tests that Foo does Xyz. 
TEST_F(PedestalTest, ComputeParametersTest) { 
    p->ComputeParameters(); 
    EXPECT_EQ(p->GetHisto()->GetEntries(),5000); 
    EXPECT_LE(std::abs(p->GetHisto()->GetMean()),1); 
    EXPECT_LE(p->GetHisto()->GetRMS(),4); 
} 

} // namespace 

int main(int argc, char **argv) { 
    ::testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

Makefile

# A sample Makefile for building Google Test and using it in user 
# tests. Please tweak it to suit your environment and project. You 
# may want to move it to your project's root directory. 
# 
# SYNOPSIS: 
# 
# make [all] - makes everything. 
# make TARGET - makes the given target. 
# make clean - removes all files generated by make. 

# Please tweak the following variable definitions as needed by your 
# project, except GTEST_HEADERS, which you can use in your own targets 
# but shouldn't modify. 

# Points to the root of Google Test, relative to where this file is. 
# Remember to tweak this if you move this file. 
GTEST_DIR = /home/ja/gtest 

# Where to find user code. 
USER_DIR = 

# Flags passed to the preprocessor. 
CPPFLAGS += -I$(GTEST_DIR)/include `root-config --cflags --libs` 

# Flags passed to the C++ compiler. 
CXXFLAGS += -g -Wall -Wextra 

# All tests produced by this Makefile. Remember to add new tests you 
# created to the list. 
TESTS = sample1_unittest 

# All Google Test headers. Usually you shouldn't change this 
# definition. 
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \ 
       $(GTEST_DIR)/include/gtest/internal/*.h 


# House-keeping build targets. 

all : $(TESTS) 

clean : 
    rm -f $(TESTS) gtest.a gtest_main.a *.o 

# Builds gtest.a and gtest_main.a. 

# Usually you shouldn't tweak such internal variables, indicated by a 
# trailing _. 
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS) 

# For simplicity and to avoid depending on Google Test's 
# implementation details, the dependencies specified below are 
# conservative and not optimized. This is fine as Google Test 
# compiles fast and for ordinary users its source rarely changes. 
gtest-all.o : $(GTEST_SRCS_) 
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ 
      $(GTEST_DIR)/src/gtest-all.cc 

gtest_main.o : $(GTEST_SRCS_) 
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \ 
      $(GTEST_DIR)/src/gtest_main.cc 

gtest.a : gtest-all.o 
    $(AR) $(ARFLAGS) [email protected] $^ 

gtest_main.a : gtest-all.o gtest_main.o 
    $(AR) $(ARFLAGS) [email protected] $^ 

# Builds a sample test. A test should link with either gtest.a or 
# gtest_main.a, depending on whether it defines its own main() 
# function. 

sample1.o : $(USER_DIR)/ModuleTest.cc $(GTEST_HEADERS) 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/ModuleTest.cc 

sample1_unittest.o : $(USER_DIR)/ModuleTest.cc \ 
         $(GTEST_HEADERS) 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/ModuleTest.cc 

sample1_unittest : ModuleTest.o sample1_unittest.o gtest_main.a 
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o [email protected] 

и, наконец, компиляцией выход

g++ -g -Wall -Wextra -I/home/ja/gtest/include `root-config --cflags --libs` -c -o ModuleTest.o ModuleTest.cc 
ModuleTest.cc:29:1: error: expected class-name before ‘{’ token 
ModuleTest.cc:29:1: error: ‘PedestalTest’ was not declared in this scope 
ModuleTest.cc:29:1: error: no matching function for call to ‘GetTypeId()’ 
ModuleTest.cc:29:1: note: candidate is: 
In file included from /home/ja/gtest/include/gtest/gtest.h:57:0, 
       from ModuleTest.cc:2: 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:492:8: note: template<class T> const void* testing::internal::GetTypeId() 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:492:8: note: template argument deduction/substitution failed: 
ModuleTest.cc:29:1: error: template argument 1 is invalid 
ModuleTest.cc:29:1: error: ‘PedestalTest’ has not been declared 
ModuleTest.cc:29:1: error: ‘PedestalTest’ has not been declared 
ModuleTest.cc: In member function ‘virtual void {anonymous}::PedestalTest_ShowRAWTest_Test::TestBody()’: 
ModuleTest.cc:30:3: error: ‘p’ was not declared in this scope 
ModuleTest.cc:31:3: error: template argument 1 is invalid 
ModuleTest.cc: At global scope: 
ModuleTest.cc:35:1: error: expected class-name before ‘{’ token 
ModuleTest.cc:35:1: error: ‘PedestalTest’ was not declared in this scope 
ModuleTest.cc:35:1: error: no matching function for call to ‘GetTypeId()’ 
ModuleTest.cc:35:1: note: candidate is: 
In file included from /home/ja/gtest/include/gtest/gtest.h:57:0, 
       from ModuleTest.cc:2: 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:492:8: note: template<class T> const void* testing::internal::GetTypeId() 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:492:8: note: template argument deduction/substitution failed: 
ModuleTest.cc:35:1: error: template argument 1 is invalid 
ModuleTest.cc:35:1: error: ‘PedestalTest’ has not been declared 
ModuleTest.cc:35:1: error: ‘PedestalTest’ has not been declared 
ModuleTest.cc: In member function ‘virtual void {anonymous}::PedestalTest_ComputeParametersTest_Test::TestBody()’: 
ModuleTest.cc:36:3: error: ‘p’ was not declared in this scope 
ModuleTest.cc:37:3: error: template argument 1 is invalid 
In file included from /home/ja/gtest/include/gtest/gtest.h:57:0, 
       from ModuleTest.cc:2: 
/home/ja/gtest/include/gtest/internal/gtest-internal.h: In instantiation of ‘testing::Test* testing::internal::TestFactoryImpl<TestClass>::CreateTest() [with TestClass = {anonymous}::PedestalTest_ComputeParametersTest_Test]’: 
ModuleTest.cc:47:1: required from here 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:529:43: error: cannot convert ‘{anonymous}::PedestalTest_ComputeParametersTest_Test*’ to ‘testing::Test*’ in return 
/home/ja/gtest/include/gtest/internal/gtest-internal.h: In instantiation of ‘testing::Test* testing::internal::TestFactoryImpl<TestClass>::CreateTest() [with TestClass = {anonymous}::PedestalTest_ShowRAWTest_Test]’: 
ModuleTest.cc:47:1: required from here 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:529:43: error: cannot convert ‘{anonymous}::PedestalTest_ShowRAWTest_Test*’ to ‘testing::Test*’ in return 
/home/ja/gtest/include/gtest/internal/gtest-internal.h: In member function ‘testing::Test* testing::internal::TestFactoryImpl<TestClass>::CreateTest() [with TestClass = {anonymous}::PedestalTest_ShowRAWTest_Test]’: 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:529:54: warning: control reaches end of non-void function [-Wreturn-type] 
/home/ja/gtest/include/gtest/internal/gtest-internal.h: In member function ‘testing::Test* testing::internal::TestFactoryImpl<TestClass>::CreateTest() [with TestClass = {anonymous}::PedestalTest_ComputeParametersTest_Test]’: 
/home/ja/gtest/include/gtest/internal/gtest-internal.h:529:54: warning: control reaches end of non-void function [-Wreturn-type] 
make: *** [ModuleTest.o] Error 1 

Пожалуйста, дайте мне какие-либо советы, что я должен сделать, чтобы исправить всю ошибку. С наилучшими пожеланиями.

ответ

3

В ModuleTest.cc, похоже, вам необходимо либо сделать:

class ModuleTest : public ::testing::Test 
class PedestalTest : public ::testing::Test 

или:

TEST_F(PedestalTest,ShowRAWTest) { 
TEST_F(ModuleTest, ShowRAWTest) { 
... 
} 

TEST_F(PedestalTest, ComputeParametersTest) { 
TEST_F(ModuleTest, ComputeParametersTest) { 
... 
} 
+0

Он работает, спасибо большое! – user1877600

Смежные вопросы