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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
#include <CLAM/InControl.hxx>
#include <CLAM/OutControl.hxx>
#include <CLAM/OutControlRegistry.hxx>
#include <CLAM/Processing.hxx>
#include <cppunit/extensions/HelperMacros.h>
#include "BaseLoggable.hxx"
#include <string>
namespace CLAMTest {
class TypedControlsTest;
CPPUNIT_TEST_SUITE_REGISTRATION( TypedControlsTest );
class TypedControlsTest : public CppUnit::TestFixture, public BaseLoggable, public CLAM::Processing
{
CPPUNIT_TEST_SUITE( TypedControlsTest );
// testing InControl and OutControl
CPPUNIT_TEST( testInControl_DoControl_ChangesInternalState );
CPPUNIT_TEST( testAddLinkAndSendControl_ChangesInControlInternalState );
// tests for IsConnected / IsConnectedTo
CPPUNIT_TEST( testIsConnected_WithOutControl_AfterConnection );
CPPUNIT_TEST( testIsConnected_WithOutControl_WithoutConnection );
CPPUNIT_TEST( testIsConnectedTo_WithOutControl_WhenControlsAreConnected );
CPPUNIT_TEST( testIsConnectedTo_WithOutControl_WhenControlsAreNotConnected );
CPPUNIT_TEST( testInControlIsConnected_byDefault );
CPPUNIT_TEST( testInControlIsConnected_whenConnected );
CPPUNIT_TEST( testInControlIsConnected_whenDisconnected );
// Destructor disconnections
CPPUNIT_TEST( testInControlDestructor_disconnectsOutControl );
CPPUNIT_TEST( testOutControlDestructor_disconnectsInControl );
// 1 Out - Many Ins Test
CPPUNIT_TEST( testOneOutControlManyInControls_DoControl_ChangesInternalState );
// Template Link Tests
CPPUNIT_TEST( testIsLinkable_withDifferentControls );
CPPUNIT_TEST( testIsLinkable_withSameControls );
CPPUNIT_TEST( testAddLink_withSameControls );
CPPUNIT_TEST( testAddLink_withDifferentControls );
CPPUNIT_TEST( testIsConnected_WithOutControl_AfterConnection_withFloat );
CPPUNIT_TEST( testIsConnected_WithOutControl_WithNoConnection_withFloat );
CPPUNIT_TEST( testIsConnectedTo_WithOutControl_WhenControlsAreConnected_withFloat );
CPPUNIT_TEST( testIsConnectedTo_WithOutControl_WhenControlsAreNotConnected_withFloat );
// testing InControl Callbacks
CPPUNIT_TEST( testInControlWithCallback_DoControl_ChangesInternalState );
CPPUNIT_TEST( testLinkAndSendWithInControl_CallbackMethodGetsCalled );
CPPUNIT_TEST( testLinkAndSendWithInControl_CopyCallbackMethodGetsCalled );
CPPUNIT_TEST( testControlHandlerId_WritesToLog );
CPPUNIT_TEST( testLinkAndSendWithInControl_CallbackWithIdMethodGetsCalled );
CPPUNIT_TEST( testLinkAndSendWithInControl_CopyCallbackWithIdMethodGetsCalled );
// *ControlRegistry Tests
CPPUNIT_TEST( testInControlRegistry_ProcessingInterface_Register_ChangesInternalState );
CPPUNIT_TEST( testOutControlRegistry_ProcessingInterface_Register_ChangesInternalState );
CPPUNIT_TEST( testInControlRegistry_Has_loadingControlWithSameName );
CPPUNIT_TEST( testOutControlRegistry_Has_loadingControlWithSameName );
CPPUNIT_TEST( testInControlRegistry_Has_withNoControls );
CPPUNIT_TEST( testOutControlRegistry_Has_withNoControls );
// Testing bounds
CPPUNIT_TEST( testInControl_doesntHaveBoundsBydefault );
CPPUNIT_TEST( testInControl_defaultBounds );
CPPUNIT_TEST( testInControl_settingBounds );
CPPUNIT_TEST( testInControl_boundedDefaultValue );
CPPUNIT_TEST( testInControl_isBoundedWhenTrue );
// Testing default value
CPPUNIT_TEST( testInControl_setDefaultprevailstoBounds );
CPPUNIT_TEST_SUITE_END();
// Testing pattern: Self Shunt
// Processing interface:
const char* GetClassName() const { return "for testing"; }
bool Do() { return false; }
public:
void tearDown()
{
ClearLog();
}
// testing InControl and OutControl
void testInControl_DoControl_ChangesInternalState()
{
CLAM::InControl<int> in("IntInControl");
in.DoControl(1);
CPPUNIT_ASSERT_EQUAL( 1, in.GetLastValue() );
}
void testAddLinkAndSendControl_ChangesInControlInternalState()
{
CLAM::InControl<int> in("IntInControl");
CLAM::OutControl<int> out("IntOutControl");
out.AddLink(in);
out.SendControl(1);
CPPUNIT_ASSERT_EQUAL( 1 , in.GetLastValue() );
}
// tests for IsConnected / IsConnectedTo
void testIsConnected_WithOutControl_AfterConnection()
{
CLAM::InControl<int> in("IntInControl");
CLAM::OutControl<int> out("IntOutControl");
out.AddLink(in);
CPPUNIT_ASSERT_EQUAL( true, out.IsConnected() );
}
void testIsConnected_WithOutControl_WithoutConnection()
{
CLAM::OutControl<int> out("IntOutControl");
CPPUNIT_ASSERT_EQUAL( false, out.IsConnected() );
}
void testIsConnectedTo_WithOutControl_WhenControlsAreConnected()
{
CLAM::InControl<int> in("IntInControl");
CLAM::OutControl<int> out("IntOutControl");
out.AddLink(in);
CPPUNIT_ASSERT_EQUAL( true, out.IsConnectedTo(in) );
}
void testIsConnectedTo_WithOutControl_WhenControlsAreNotConnected()
{
CLAM::InControl<int> in("IntInControl");
CLAM::OutControl<int> out("IntOutControl");
CPPUNIT_ASSERT_EQUAL( false, out.IsConnectedTo(in) );
}
void testInControlIsConnected_byDefault()
{
CLAM::InControl<int> in("IntInControl");
CPPUNIT_ASSERT_EQUAL(false, in.IsConnected());
}
void testInControlIsConnected_whenConnected()
{
CLAM::InControl<int> in("IntInControl");
CLAM::OutControl<int> out("IntOutControl");
out.AddLink(in);
CPPUNIT_ASSERT_EQUAL(true, in.IsConnected());
}
void testInControlIsConnected_whenDisconnected()
{
CLAM::InControl<int> in("IntInControl");
CLAM::OutControl<int> out("IntOutControl");
out.AddLink(in);
out.RemoveLink(in);
CPPUNIT_ASSERT_EQUAL(false, in.IsConnected());
}
// Destructor disconnections
void testInControlDestructor_disconnectsOutControl()
{
CLAM::OutControl<int> out("IntOutControl");
{
CLAM::InControl<int> in("IntInControl");
out.AddLink(in);
}
CPPUNIT_ASSERT_EQUAL(false, out.IsConnected());
}
void testOutControlDestructor_disconnectsInControl()
{
CLAM::InControl<int> in("IntInControl");
{
CLAM::OutControl<int> out("IntOutControl");
out.AddLink(in);
}
CPPUNIT_ASSERT_EQUAL(false, in.IsConnected());
}
// 1 Out - Many Ins Test
void testOneOutControlManyInControls_DoControl_ChangesInternalState()
{
CLAM::OutControl<int> out("IntOutControl");
CLAM::InControl<int> in1("IntInControl 1");
CLAM::InControl<int> in2("IntInControl 2");
out.AddLink(in1);
out.AddLink(in2);
out.SendControl(1);
CPPUNIT_ASSERT_EQUAL( 1 , in1.GetLastValue() );
CPPUNIT_ASSERT_EQUAL( 1 , in2.GetLastValue() );
}
// Template Link Tests
void testIsLinkable_withDifferentControls()
{
CLAM::InControl<int> concreteIn("Concrete In");
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
CPPUNIT_ASSERT_EQUAL(false, out.IsLinkable(in));
}
void testIsLinkable_withSameControls()
{
CLAM::InControl<float> concreteIn("Concrete In");
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
CPPUNIT_ASSERT_EQUAL(true, out.IsLinkable(in));
}
void testAddLink_withDifferentControls()
{
CLAM::InControl<float> concreteIn("Concrete In");
CLAM::OutControl<int> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
try {
out.AddLink(in);
CPPUNIT_FAIL("assertion failed expected, but nothing happened");
} catch(CLAM::ErrAssertionFailed& ) {}
}
void testAddLink_withSameControls()
{
CLAM::InControl<float> concreteIn("Concrete In");
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
out.AddLink(in);
CPPUNIT_ASSERT_EQUAL(true, concreteOut.IsConnectedTo(concreteIn));
}
void testIsConnected_WithOutControl_AfterConnection_withFloat()
{
CLAM::InControl<float> concreteIn("Concrete In");
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
out.AddLink(in);
CPPUNIT_ASSERT_EQUAL(true, out.IsConnected());
}
void testIsConnected_WithOutControl_WithNoConnection_withFloat()
{
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::OutControlBase & out = concreteOut;
CPPUNIT_ASSERT_EQUAL(false, out.IsConnected());
}
void testIsConnectedTo_WithOutControl_WhenControlsAreConnected_withFloat()
{
CLAM::InControl<float> concreteIn("Concrete In");
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
out.AddLink(in);
CPPUNIT_ASSERT_EQUAL(true, out.IsConnectedTo(in));
}
void testIsConnectedTo_WithOutControl_WhenControlsAreNotConnected_withFloat()
{
CLAM::InControl<float> concreteIn("Concrete In");
CLAM::OutControl<float> concreteOut("Concrete Out");
CLAM::InControlBase & in = concreteIn;
CLAM::OutControlBase & out = concreteOut;
CPPUNIT_ASSERT_EQUAL(false, out.IsConnectedTo(in));
}
// testing CascadingInControl
void testInControlWithCallback_DoControl_ChangesInternalState()
{
CLAM::InControl<int>
in("in", this, &TypedControlsTest::ControlHandler);
in.DoControl(1);
CPPUNIT_ASSERT_EQUAL( 1, in.GetLastValue() );
}
// helper method used for handling incoming control
void ControlHandler(const int & val) {
ToLog() << "ControlHandler called with: " << val;
}
void ControlCopyHandler(int val) {
ToLog() << "ControlHandler called with: " << val;
}
void testLinkAndSendWithInControl_CallbackMethodGetsCalled()
{
CLAM::InControl<int>
in("in", this, &TypedControlsTest::ControlHandler);
in.DoControl(1);
CPPUNIT_ASSERT_EQUAL( std::string("ControlHandler called with: 1"), GetLog() );
}
void testLinkAndSendWithInControl_CopyCallbackMethodGetsCalled()
{
CLAM::InControl<int>
in("in", this, &TypedControlsTest::ControlCopyHandler);
in.DoControl(1);
CPPUNIT_ASSERT_EQUAL( std::string("ControlHandler called with: 1"), GetLog() );
}
// helper method for handling incoming control plus incontrol ID
void ControlHandlerId(unsigned id, const int & val) {
ToLog() << "ControlHandler called with id : " << id << " and value : " << val;
}
void ControlCopyHandlerId(unsigned id, int val) {
ToLog() << "ControlHandler called with id : " << id << " and value : " << val;
}
void testControlHandlerId_WritesToLog()
{
ControlHandlerId(0, 1);
CPPUNIT_ASSERT_EQUAL( std::string("ControlHandler called with id : 0 and value : 1"), GetLog() );
}
void testLinkAndSendWithInControl_CallbackWithIdMethodGetsCalled()
{
const unsigned controlId=2;
CLAM::InControl<int>
in( controlId, "in", this, &TypedControlsTest::ControlHandlerId );
in.DoControl( 1 );
CPPUNIT_ASSERT_EQUAL(
GetLog(),
std::string("ControlHandler called with id : 2 and value : 1") );
// note that controlId == 2
}
void testLinkAndSendWithInControl_CopyCallbackWithIdMethodGetsCalled()
{
const unsigned controlId=2;
CLAM::InControl<int>
in( controlId, "in", this, &TypedControlsTest::ControlCopyHandlerId );
in.DoControl( 1 );
CPPUNIT_ASSERT_EQUAL(
GetLog(),
std::string("ControlHandler called with id : 2 and value : 1") );
// note that controlId == 2
}
// *ControlRegistry Tests
// testing InControl and OutControl
void testInControlRegistry_ProcessingInterface_Register_ChangesInternalState()
{
CLAM::InControl<int> concreteIn("IntInControl");
CLAM::InControlBase & in = concreteIn;
CLAM::InControlRegistry inRegistry;
inRegistry.ProcessingInterface_Register(&in);
CPPUNIT_ASSERT_EQUAL( &in, &inRegistry.Get("IntInControl") );
}
void testOutControlRegistry_ProcessingInterface_Register_ChangesInternalState()
{
CLAM::OutControl<int> concreteOut("IntOutControl");
CLAM::OutControlBase & out = concreteOut;
CLAM::OutControlRegistry outRegistry;
outRegistry.ProcessingInterface_Register(&out);
CPPUNIT_ASSERT_EQUAL( &out, &outRegistry.Get("IntOutControl") );
}
void testInControlRegistry_Has_loadingControlWithSameName()
{
CLAM::InControl<int> concreteIn("IntInControl");
CLAM::InControlBase & in = concreteIn;
CLAM::InControlRegistry inRegistry;
inRegistry.ProcessingInterface_Register(&in);
CPPUNIT_ASSERT_EQUAL( true, inRegistry.Has("IntInControl") );
}
void testOutControlRegistry_Has_loadingControlWithSameName()
{
CLAM::OutControl<int> concreteOut("IntOutControl");
CLAM::OutControlBase & out = concreteOut;
CLAM::OutControlRegistry outRegistry;
outRegistry.ProcessingInterface_Register(&out);
CPPUNIT_ASSERT_EQUAL( true, outRegistry.Has("IntOutControl") );
}
void testInControlRegistry_Has_withNoControls()
{
CLAM::InControlRegistry inRegistry;
CPPUNIT_ASSERT_EQUAL( false, inRegistry.Has("IntInControl") );
}
void testOutControlRegistry_Has_withNoControls()
{
CLAM::OutControlRegistry outRegistry;
CPPUNIT_ASSERT_EQUAL( false, outRegistry.Has("IntOutControl") );
}
void testInControl_doesntHaveBoundsBydefault()
{
CLAM::InControl<float> concreteIn("InControl");
CLAM::InControlBase & inControl = concreteIn;
CPPUNIT_ASSERT_EQUAL( false, inControl.IsBounded() );
}
void testInControl_defaultBounds()
{
CLAM::InControl<float> concreteIn("InControl");
CLAM::InControlBase & inControl = concreteIn;
CPPUNIT_ASSERT_EQUAL( 0.0f, inControl.LowerBound() );
CPPUNIT_ASSERT_EQUAL( 1.0f, inControl.UpperBound() );
}
void testInControl_settingBounds()
{
CLAM::InControl<float> concreteIn("InControl");
CLAM::InControlBase & inControl = concreteIn;
inControl.SetBounds(-1.f, 2.f);
CPPUNIT_ASSERT_EQUAL( -1.f, inControl.LowerBound() );
CPPUNIT_ASSERT_EQUAL( 2.f, inControl.UpperBound() );
}
void testInControl_boundedDefaultValue()
{
CLAM::InControl<float> concreteIn("InControl");
CLAM::InControlBase & inControl = concreteIn;
inControl.SetBounds(0.f, 10.f);
CPPUNIT_ASSERT_EQUAL( 5.f, inControl.DefaultValue() );
}
void testInControl_isBoundedWhenTrue()
{
CLAM::InControl<float> concreteIn("InControl");
CLAM::InControlBase & inControl = concreteIn;
inControl.SetBounds(0.f, 0.f);
CPPUNIT_ASSERT_EQUAL( true, inControl.IsBounded() );
}
void testInControl_setDefaultprevailstoBounds()
{
CLAM::InControl<float> concreteIn("InControl");
CLAM::InControlBase & inControl = concreteIn;
inControl.SetBounds(0.f, 10.f);
inControl.SetDefaultValue(0.0f);
CPPUNIT_ASSERT_EQUAL( 0.0f, inControl.DefaultValue() );
}
};
} // namespace
|