简体   繁体   English

Boost单元测试和Visual Studio 2005 / Visual C ++和BOOST_AUTO_TEST_SUITE(stringtest)命名空间?

[英]Boost Unit Testing and Visual Studio 2005/Visual C++ and the BOOST_AUTO_TEST_SUITE(stringtest) namespace?

I'm reading this article on the Boost Unit Testing Framework. 我读这篇文章的升压单元测试框架。

However I'm having a bit of trouble with the first example, my guess is that they left something out (something that would be obvious to hardcore C++ coders) as IBM often does in their articles. 但是我在第一个示例中遇到了麻烦,我的猜测是他们像IBM在他们的文章中经常做的那样遗漏了一些东西(这对核心C ++编码人员来说是显而易见的)。 Another possibility is that my Visual Studio 2005 C++ compiler is just too old for the example. 另一种可能是我的Visual Studio 2005 C ++编译器对于该示例而言太旧了。

#include "stdafx.h"
#define BOOST_TEST_MODULE stringtest
#include <boost/test/unit_test.hpp>
//#include "mystring.h"

BOOST_AUTO_TEST_SUITE(stringtest) // name of the test suite is stringtest

BOOST_AUTO_TEST_CASE(test1)
{
  /*
  mystring s;
  BOOST_CHECK(s.size() == 0);
  */
  BOOST_CHECK(0 == 0);
}

BOOST_AUTO_TEST_CASE(test2)
{
  /*
  mystring s;
  s.setbuffer("hello world");
  BOOST_REQUIRE_EQUAL('h', s[0]); // basic test
  */
   BOOST_CHECK(0 == 0);
}

BOOST_AUTO_TEST_SUITE_END()

To me the BOOST_AUTO_TEST_SUITE and BOOST_AUTO_TEST_CASE lines look a little suspect (especially since they don't have quotes around the arguments, and they are undeclared identifiers...but this probably means they are macros and I'm not certain I understand the concept or if that is available in VC++ 8.0)... 对我而言,BOOST_AUTO_TEST_SUITE和BOOST_AUTO_TEST_CASE行看起来有点可疑(尤其是因为它们在参数周围没有引号,并且它们是未声明的标识符...但是这可能意味着它们是宏,并且不确定我是否理解这个概念或如果在VC ++ 8.0中可用)...

#ifdef _MYSTRING
#define _MYSTRING

class mystring {
   char* buffer;
   int length;
   public:
      void setbuffer(char* s) { buffer s = s; length = strlen(s); }
      char& operator[ ] (const int index) { return buffer[index]; }
      int size() {return length; }
}

#endif

Is there any reason why this code won't work? 有什么原因导致此代码无法正常工作?

1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(7) : error C2065: 'stringtest' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C2146: syntax error : missing ';' before identifier 'BOOST_AUTO_TEST_CASE'
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C2065: 'test1' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(10) : error C2448: 'BOOST_AUTO_TEST_CASE' : function-style initializer appears to be a function definition
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(18) : error C2065: 'test2' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(19) : error C2448: 'BOOST_AUTO_TEST_CASE' : function-style initializer appears to be a function definition
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(29) : fatal error C1004: unexpected end-of-file found

Looks correct to me. 对我来说看起来正确。 My Boost.Test code looks the same way. 我的Boost.Test代码看起来是一样的。 I'm running VS2008, but I know it works in 2005 as well. 我正在运行VS2008,但我知道它也可以在2005年使用。

Seems like your problem lies elsewhere. 似乎您的问题出在其他地方。 If you use precompiled headers (and why do you do that in such a small test program?), shouldn't stdafx.h be included as the very first thing in the file? 如果您使用预编译的头文件(为什么在如此小的测试程序中这样做呢?),stdafx.h不应作为文件的第一件事包括在内吗?

And what is the first line for? 第一行是什么? You don't seem to use it, and _MYSTRING is a reserved name in C++ (everything that begins with underscore followed by a capital letter is off limits) 您似乎没有使用它,并且_MYSTRING是C ++中的保留名称(以下划线开头,大写字母开头的所有内容_MYSTRING使用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM