使用 Boost.Test

时间:2021-02-10 19:27:39

标签: c++ boost makefile boost.test

我正在尝试找出 Boost.Test,这样我就可以从一开始就开始使用单元测试来处理我将要编写的新项目。首先,我编写了一个名为 Centroid 的类。

质心.h

#ifndef CENTROID_H
#define CENTROID_H

#include "pixel.h"
#include <vector>

class Centroid
{
  // Each centroid has a unique ID
  int m_id{};

  // A vector of all pixels owned by this centroid
  std::vector<Pixel> m_ownedPixels{};

public:

  //Default constructor
  Centroid();

  // Constructor
  Centroid(std::vector<Pixel>& pixelVector, int id);

  // Destructor
  ~Centroid();

  // Adds a pixel to m_ownedPixels
  void addPixel(Pixel& p);
};
#endif

质心.cpp

#include "centroid.h"
#include "pixel.h"

#include <vector>

Centroid::Centroid(std::vector<Pixel>& pixelVector, int id)
{

  m_id = id;
  for(auto& pixel : pixelVector)
    {
      pixel.ownedBy = m_id;
      addPixel(pixel);
    }
}

Centroid::Centroid()
{
}

Centroid::~Centroid()
{
}

void Centroid::addPixel(Pixel& p)
{
  p.ownedBy = m_id;
  m_ownedPixels.push_back(p);
}

还有一个叫做 pixel 的结构

像素.h

#ifndef PIXEL_H
#define PIXEL_H

struct Pixel
{
    double r{};
    double g{};
    double b{};
    int id{};
    int ownedBy{};
};
#endif

然后我创建了一个名为 test_centroid.cpp 的文件。这是给我带来麻烦的。

#include "../src/centroid.h"
#include "../src/pixel.h"
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Centroid test

#include <boost/test/unit_test.hpp>

#include <vector>

BOOST_AUTO_TEST_SUITE(Centroid_test)

BOOST_AUTO_TEST_CASE(centroid1)
{
  std::vector<Pixel> pixelVector{};
  for(int i{0}; i < 5; ++i)
    {
      pixelVector.push_back(Pixel{0,0,0,i});
    }

  Centroid centroid{};
}

BOOST_AUTO_TEST_SUITE_END()

当然还有我的主文件,它什么都不做

#include "centroid.h"
#include "pixel.h"


#include <iostream>
#include <vector>


int main()
{
  return 0;
}

我也有这些 makefile。请在这里放轻松,我对编写 makefile 很陌生。

这个在我的根目录下

#---------------
#
# Compiler things
#
#---------------

CXX=g++
COMPILE_FLAGS=-ggdb -Wall -std=c++14 -c
LINK_FLAGS=-ggdb -Wall -std=c++14
LIBS=-L/usr/local/lib/OpenCV -L/usr/lib/x86_64-linux-gnu
INCLUDE=-I/usr/local/include/opencv4
LINKER=-l:libopencv_core.so -l:libopencv_imgproc.so -l:libopencv_imgcodecs.so \
-l:libopencv_video.so -l:libopencv_videoio.so # for later
TEST_LINKER=-l:libboost_unit_test_framework.so


#---------------
#
# Directories
#
#---------------

ROOT_DIR=/home/user/Code/ImageProcessing
SRC_DIR=$(ROOT_DIR)/src
TEST_DIR=$(ROOT_DIR)/t


export CXX
export COMPILE_FLAGS
export LINK_FLAGS
export LIBS
export INCLUDE
export LINKER
export TEST_LINKER
export ROOT_DIR

#---------------
#
# Targets
#
#---------------

all: main test

main:
    @$(MAKE) -C $(SRC_DIR)

test: main
    @$(MAKE) -C $(TEST_DIR)

test_clean:
    @$(MAKE) -C $(TEST_DIR) clean

clean: test_clean
    @$(RM) $(SRC_DIR)/*.o $(SRC_DIR)/*.run *.run *.o

此生成文件位于文件夹 ./t

#---------------
#
# Targets
#
#---------------

all: test_centroid.o
    @$(CXX) $(LINK_FLAGS) $(LIBS) $(INCLUDE) test_centroid.o -o run_tests.run $(TEST_LINKER); \ 
    cp run_tests.run $(ROOT_DIR)

test_centroid.o: test_centroid.cpp
    @$(CXX) $(COMPILE_FLAGS) $(LIBS) $(INCLUDE) test_centroid.cpp

clean:
    @$(RM) *.o *.run   

最后(我认为最不重要)这个 makefile 位于 ./src

#---------------
#
# Targets
#
#---------------


all: main.o centroid.o
    @$(CXX) $(LINK_FLAGS) $(LIBS) $(INCLUDE) main.o centroid.o -o main.run $(LINKER); \ 
    cp main.run $(ROOT_DIR)

main.o: main.cpp
    @$(CXX) $(COMPILE_FLAGS) $(LIBS) $(INCLUDE) main.cpp

centroid.o: centroid.h centroid.cpp
    @$(CXX) $(COMPILE_FLAGS) $(LIBS) $(INCLUDE) centroid.cpp

我的文件夹结构是这样的

root/
├── Makefile
├── src/
│   ├── centroid.cpp
│   ├── centroid.h
│   ├── main.cpp
│   ├── Makefile
│   └── pixel.h
└── t/
    ├── Makefile
    └── test_centroid.cpp

我意识到我的 boost 测试实际上还没有测试任何东西,但它不会编译,除非我删除行 Centroid centroid{}; 到目前为止,当我从根目录运行 make 时,它​​给了我以下错误

make[1]: Entering directory '/home/user/Code/ImageProcessing/src'
make[1]: Leaving directory '/home/user/Code/ImageProcessing/src'
make[1]: Entering directory '/home/user/Code/ImageProcessing/t'
test_centroid.o: In function `Centroid_test::centroid1::test_method()':
/home/user/Code/ImageProcessing/t/test_centroid.cpp:20: undefined reference to `Centroid::Centroid()'
/home/user/Code/ImageProcessing/t/test_centroid.cpp:20: undefined reference to `Centroid::~Centroid()'
collect2: error: ld returned 1 exit status
cp: cannot stat 'run_tests.run': No such file or directory
Makefile:8: recipe for target 'all' failed
make[1]: *** [all] Error 1
make[1]: Leaving directory '/home/user/Code/ImageProcessing/t'
Makefile:47: recipe for target 'test' failed
make: *** [test] Error 2

我见过一千次未定义的引用错误,但通常它们是针对外部库的,因为我忘记链接 .so 或 .a。这次我不知所措。它是否希望我将我的类构建为一个库,然后在 test_centroid.o 的链接阶段链接到它?

如果有帮助,我使用的是 Elementary OS 5.1.6,我使用的是 4.1 版的 make、7.5.0 的 g++ 和 1.65.0 的 boost。

0 个答案:

没有答案