File: igtlMessageFactoryTest.cxx

package info (click to toggle)
openigtlink 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 20,076; ansic: 6,704; sh: 227; perl: 74; makefile: 46
file content (90 lines) | stat: -rw-r--r-- 2,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*=========================================================================

  Program:   The OpenIGTLink Library
  Language:  C++
  Web page:  http://openigtlink.org/

  Copyright (c) Insight Software Consortium. All rights reserved.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include <igtlMessageFactory.h>
#include <igtlMessageHeader.h>
#include <igtlTransformMessage.h>

#include <stdexcept>

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

namespace igtl
{
  // Create a nonsense message type that won't exist in main library.
  igtlCreateDefaultQueryMessageClass(BananaMessage, "BANANA")
}

/**
 * Author: Matt Clarkson (m.clarkson@ucl.ac.uk).
 */
int main(int , char * [] )
{

  igtl::MessageFactory::Pointer factory = igtl::MessageFactory::New();

  igtl::MessageHeader::Pointer header = NULL;

  if (factory->IsValid(header))
    {
    std::cerr << "A null header is not valid." << std::endl;
    return EXIT_FAILURE;
    }

  header = igtl::MessageHeader::New();

  if (factory->IsValid(header))
    {
    std::cerr << "A header without a DeviceType e.g. STRING, TRANSFORM is invalid." << std::endl;
    return EXIT_FAILURE;
    }

  igtl::TransformMessage::Pointer transformMessage = igtl::TransformMessage::New();

  if (!factory->IsValid(transformMessage.GetPointer()))
    {
    std::cerr << "The IsValid method should check for not null, and a valid DeviceType. TRANSFORM should be valid." << std::endl;
    return EXIT_FAILURE;
    }

  // Test with an invalid message.
  try
    {
    igtl::BananaMessage::Pointer bananaMessage = igtl::BananaMessage::New();

    // Check firstly its not valid.
    if (factory->IsValid(bananaMessage.GetPointer()))
      {
      std::cerr << "The IsValid method should fail for BANANA messages." << std::endl;
      return EXIT_FAILURE;
      }

    factory->GetMessage(bananaMessage.GetPointer());
    throw std::logic_error("Should not reach this line, as the previous line should throw invalid_argument, as BANANA messages are not valid.");
    }
  catch (const std::invalid_argument& e)
    {
    std::cerr << "Caught exception e=" << e.what() << std::endl;
    }
  catch (const std::exception& e)
    {
    std::cerr << "Caught std::exception, which should not happen. e=" << e.what() << std::endl;
    throw e;
    }


  return EXIT_SUCCESS;
}