views:

242

answers:

1

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. Another possibility is that my Visual Studio 2005 C++ compiler is just too old for the example.

#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)...

#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
+1  A: 

Looks correct to me. My Boost.Test code looks the same way. I'm running VS2008, but I know it works in 2005 as well.

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?

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)

jalf
Putting that stdafx.h as the first line in the file really seemed to clear up a lot of the errors, now it's down to just one:1>Linking...1>LINK : fatal error LNK1104: cannot open file 'libboost_unit_test_framework-vc80-mt-gd-1_40.lib'
leeand00
The file libboost_unit_test_framework-vc80-mt-gd-1_40.lib is indeed located in my C:\Program Files (x86)\boost\boost_1_40\lib directory.
leeand00
And is that path added to the project's (or visual studio's) linker path?
jalf
Yeah I just did that. Here's the other post related to that for anyone who's interested: http://stackoverflow.com/questions/212492/how-do-you-add-external-libraries-for-compilation-in-vc
leeand00
@jalf Wow with a rep like that you must have an unhealthy fascination with C++.
leeand00
I didn't get all of it from C++ questions - but yes, you're probably right. ;)Anyway, it can't find the .lib file. Either the path is not properly added to the project properties (check project properties->linker->command line to make sure it is added and passed correctly to the compiler), or the file isn't there.
jalf
@jalf That first line is part of the example I think...:-p there's another part to the example....wait let me add it...
leeand00
@jalf Editited just to include the unit test code...(it compiles that way, but when I uncomment string calls within the unit tests it doesn't compile though.
leeand00