summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/unit/Trading/Interpreter/Interpreter.cpp
blob: edc28721c2cf53550fc54e1744bcfb8149935df0 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// $Id$

#include "orbsvcs/Trader/Interpreter.h"
#include "orbsvcs/Trader/Constraint_Visitors.h"
#include "ace/OS_NS_string.h"

template<class T> class Literal_Interpreter;

template<class T>
class Literal_Visitor: public TAO_Constraint_Visitor
{
public:
  Literal_Visitor (Literal_Interpreter<T>* interp);
  virtual ~Literal_Visitor (void);

  virtual int visit_constraint (TAO_Unary_Constraint*);

  virtual int visit_with (TAO_Unary_Constraint*) { return 1; }
  virtual int visit_min (TAO_Unary_Constraint*) { return 1; }  
  virtual int visit_max (TAO_Unary_Constraint*) { return 1; }  
  virtual int visit_first (TAO_Noop_Constraint*) { return 1; }
  virtual int visit_random (TAO_Noop_Constraint*) { return 1; }

  virtual int visit_and (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_or (TAO_Binary_Constraint*) { return 1; }  
  virtual int visit_not (TAO_Unary_Constraint*) { return 1; }   

  virtual int visit_exist (TAO_Unary_Constraint*) { return 1; }
  virtual int visit_unary_minus (TAO_Unary_Constraint*) { return 1; }

  virtual int visit_add (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_sub (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_mult (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_div (TAO_Binary_Constraint*) { return 1; }  

  virtual int visit_twiddle (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_in (TAO_Binary_Constraint*) { return 1; }

  virtual int visit_less_than (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_less_than_equal (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_greater_than (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_greater_than_equal (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_equal (TAO_Binary_Constraint*) { return 1; }
  virtual int visit_not_equal (TAO_Binary_Constraint*) { return 1; }

  virtual int visit_literal (TAO_Literal_Constraint*);
  virtual int visit_property (TAO_Property_Constraint*) { return 1; }

private:
  Literal_Interpreter<T>* interp_;
};

template<class T>
Literal_Visitor<T>::Literal_Visitor (Literal_Interpreter<T>* interp)
 : interp_ (interp)
{
}

template<class T>
Literal_Visitor<T>::~Literal_Visitor (void)
{
}

template<class T> int
Literal_Visitor<T>::visit_constraint (TAO_Unary_Constraint* c)
{
  return c->operand ()->accept (this);
}

template<class T> int
Literal_Visitor<T>::visit_literal (TAO_Literal_Constraint* literal)
{
  this->interp_->constraint(*literal);
  return 1;
}

template<class T>
class Literal_Interpreter: public TAO_Interpreter
{
public:
  Literal_Interpreter (void);
  virtual ~Literal_Interpreter (void);

  bool test (const char* str,
             T expected);
  void constraint (TAO_Literal_Constraint& constraint);
  const TAO_Literal_Constraint& constraint (void) const;

private:
  bool compare (T expected) const;

  TAO_Literal_Constraint constraint_;
};

template<class T>
Literal_Interpreter<T>::Literal_Interpreter (void)
{
}

template<class T>
Literal_Interpreter<T>::~Literal_Interpreter (void)
{
}

template<class T> bool
Literal_Interpreter<T>::test (const char* str,
                              T expected)
{
  this->build_tree (str);
  if (this->root_ == 0)
    {
      ACE_ERROR ((LM_ERROR, "ERROR: Invalid interpreter string.\n"));
      return false;
    }

  Literal_Visitor<T> visitor (this);
  this->root_->accept (&visitor);

  return this->compare(expected);
}

template<> bool
Literal_Interpreter<const char*>::compare (const char* expected) const
{
  return (ACE_OS::strcmp(static_cast<const char*> (this->constraint_),
                         expected) == 0);
}

template<class T> bool
Literal_Interpreter<T>::compare (T expected) const
{
  return (static_cast<T> (this->constraint_) == expected);
}

template<class T> void
Literal_Interpreter<T>::constraint (TAO_Literal_Constraint& constraint)
{
  this->constraint_ = constraint;
}

template<class T> const TAO_Literal_Constraint&
Literal_Interpreter<T>::constraint (void) const
{
  return this->constraint_;
}

int
ACE_TMAIN (int, ACE_TCHAR **)
{
  int status = 0;
  {
    Literal_Interpreter<CORBA::ULongLong> u_interp;
    if (!u_interp.test ("993834343433882",
                        ACE_UINT64_LITERAL (993834343433882)))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: CORBA::ULongLong test failed.\n"));
      }

    Literal_Interpreter<CORBA::LongLong> interp;
    if (!interp.test ("-1879048193", -1879048193))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: CORBA::LongLong test failed.\n"));
      }

    // Since we're comparing signed and unsigned, the signed should get
    // promoted to unsigned.  However, it gets logically promoted, not
    // binarily.  So, a negative value is converted into zero as an
    // unsigned value.
    if (interp.constraint () > u_interp.constraint ())
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: Mixed greater than test 1 failed.\n"));
      }
  }
  {
    Literal_Interpreter<CORBA::Double> u_interp;
    if (!u_interp.test ("993834343433882.88837719", 993834343433882.88837719))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: CORBA::Double test 1 failed.\n"));
      }

    Literal_Interpreter<CORBA::Double> interp;
    if (!interp.test ("-993834343433882.88837719",
                       -993834343433882.88837719))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: CORBA::Double test 2 failed.\n"));
      }

    if (interp.constraint () > u_interp.constraint ())
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: Mixed greater than test 2 failed.\n"));
      }
  }
  {
    Literal_Interpreter<CORBA::Boolean> interp;
    if (!interp.test ("TRUE", true))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: CORBA::Boolean test 1 failed.\n"));
      }
  }
  {
    Literal_Interpreter<CORBA::Boolean> interp;
    if (!interp.test ("FALSE", false))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: CORBA::Boolean test 2 failed.\n"));
      }
  }
  {
    Literal_Interpreter<const char*> interp;
    if (!interp.test ("'This is a test'", "This is a test"))
      {
        status++;
        ACE_ERROR ((LM_ERROR, "ERROR: const char* test failed.\n"));
      }
  }
  return status;
}