コード例 #1
0
/*
 * test that DecodeVector works
 */
void BaseInflatorTest::testDecodeVector() {
  TestInflator inflator(0, PDU::ONE_BYTE);
  uint8_t data[] = {1, 2, 3, 4, 5, 6};  // the test data
  unsigned int vector = 1;
  unsigned int bytes_used = 0;
  uint8_t flags = PDU::VFLAG_MASK;

  OLA_ASSERT_FALSE(inflator.DecodeVector(flags, data, 0, &vector, &bytes_used));
  OLA_ASSERT_EQ((unsigned int) 0, vector);
  OLA_ASSERT_EQ((unsigned int) 0, bytes_used);

  data[0] = 42;
  for (unsigned int i = 1; i < sizeof(data); i++) {
    OLA_ASSERT(inflator.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 42, vector);
    OLA_ASSERT_EQ((unsigned int) 1, bytes_used);
  }

  // now make sure we can reuse the vector
  flags = 0;
  for (unsigned int i = 0; i < sizeof(data); i++) {
    OLA_ASSERT(inflator.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 42, vector);
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // resetting doesn't allow us to reuse the vector
  inflator.ResetPDUFields();
  for (unsigned int i = 0; i < sizeof(data); i++) {
    OLA_ASSERT_FALSE(inflator.DecodeVector(flags, data, i, &vector,
                                           &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, vector);
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // now try with a vector size of 2
  flags = PDU::VFLAG_MASK;
  TestInflator inflator2(0, PDU::TWO_BYTES);
  for (unsigned int i = 0; i < 2; i++) {
    OLA_ASSERT_FALSE(
        inflator2.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, vector);
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  data[0] = 0x80;
  data[1] = 0x21;
  for (unsigned int i = 2; i < sizeof(data); i++) {
    OLA_ASSERT(inflator2.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 32801, vector);
    OLA_ASSERT_EQ((unsigned int) 2, bytes_used);
  }

  // now make sure we can reuse the vector
  flags = 0;
  for (unsigned int i = 0; i < sizeof(data); i++) {
    OLA_ASSERT(inflator2.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 32801, vector);
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // resetting doesn't allow us to reuse the vector
  inflator2.ResetPDUFields();
  for (unsigned int i = 0; i < sizeof(data); i++) {
    OLA_ASSERT_FALSE(
        inflator2.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, vector);
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // now try with a vector size of 4
  flags = PDU::VFLAG_MASK;
  TestInflator inflator4(0, PDU::FOUR_BYTES);
  for (unsigned int i = 0; i < 4; i++) {
    OLA_ASSERT_FALSE(
        inflator4.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, vector);
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  data[0] = 0x01;
  data[1] = 0x21;
  data[2] = 0x32;
  data[3] = 0x45;
  for (unsigned int i = 4; i < 8; i++) {
    OLA_ASSERT(inflator4.DecodeVector(flags, data, i, &vector, &bytes_used));
    OLA_ASSERT_EQ((uint32_t) 18952773, vector);
    OLA_ASSERT_EQ((unsigned int) 4, bytes_used);
  }
}
コード例 #2
0
ファイル: SlotTest.cpp プロジェクト: basileus/ola
/**
 * Check actions are matched correctly.
 */
void SlotTest::testActionMatching() {
  Slot slot(0);

  MockAction *rising_action1 = new MockAction();
  MockAction *falling_action1 = new MockAction();
  slot.AddAction(
      ValueInterval(10, 20), rising_action1, falling_action1);

  MockAction *default_rising_action = new MockAction();
  OLA_ASSERT_FALSE(slot.SetDefaultRisingAction(default_rising_action));
  MockAction *default_falling_action = new MockAction();
  OLA_ASSERT(
      !slot.SetDefaultFallingAction(default_falling_action));

  slot.TakeAction(NULL, 10);
  rising_action1->CheckForValue(__LINE__, 10);
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  slot.TakeAction(NULL, 20);
  rising_action1->CheckForValue(__LINE__, 20);
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  slot.TakeAction(NULL, 2);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  default_falling_action->CheckForValue(__LINE__, 2);

  slot.TakeAction(NULL, 9);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  default_rising_action->CheckForValue(__LINE__, 9);
  OLA_ASSERT(default_falling_action->NoCalls());

  slot.TakeAction(NULL, 21);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  default_rising_action->CheckForValue(__LINE__, 21);
  OLA_ASSERT(default_falling_action->NoCalls());

  // add another action
  MockAction *rising_action2 = new MockAction();
  MockAction *falling_action2 = new MockAction();
  OLA_ASSERT(slot.AddAction(
        ValueInterval(30, 40), rising_action2, falling_action2));

  slot.TakeAction(NULL, 30);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  rising_action2->CheckForValue(__LINE__, 30);
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  slot.TakeAction(NULL, 35);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  rising_action2->CheckForValue(__LINE__, 35);
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  slot.TakeAction(NULL, 40);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  rising_action2->CheckForValue(__LINE__, 40);
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  // and another two actions
  MockAction *rising_action3 = new MockAction();
  OLA_ASSERT(
      slot.AddAction(ValueInterval(23, 27), rising_action3, NULL));

  MockAction *falling_action3 = new MockAction();
  OLA_ASSERT(
      slot.AddAction(ValueInterval(28, 29), NULL, falling_action3));

  slot.TakeAction(NULL, 28);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(rising_action2->NoCalls());
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(rising_action3->NoCalls());
  falling_action3->CheckForValue(__LINE__, 28);
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  slot.TakeAction(NULL, 25);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(rising_action2->NoCalls());
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(rising_action3->NoCalls());
  OLA_ASSERT(falling_action3->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  default_falling_action->CheckForValue(__LINE__, 25);

  slot.TakeAction(NULL, 27);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(rising_action2->NoCalls());
  OLA_ASSERT(falling_action2->NoCalls());
  rising_action3->CheckForValue(__LINE__, 27);
  OLA_ASSERT(falling_action3->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  OLA_ASSERT(default_falling_action->NoCalls());

  // check the default case
  slot.TakeAction(NULL, 22);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(rising_action2->NoCalls());
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(rising_action3->NoCalls());
  OLA_ASSERT(falling_action3->NoCalls());
  OLA_ASSERT(default_rising_action->NoCalls());
  default_falling_action->CheckForValue(__LINE__, 22);

  slot.TakeAction(NULL, 28);
  OLA_ASSERT(rising_action1->NoCalls());
  OLA_ASSERT(falling_action1->NoCalls());
  OLA_ASSERT(rising_action2->NoCalls());
  OLA_ASSERT(falling_action2->NoCalls());
  OLA_ASSERT(rising_action3->NoCalls());
  OLA_ASSERT(falling_action3->NoCalls());
  default_rising_action->CheckForValue(__LINE__, 28);
  OLA_ASSERT(default_falling_action->NoCalls());
}
コード例 #3
0
/*
 * Test that DecodeLength works
 */
void BaseInflatorTest::testDecodeLength() {
  TestInflator inflator;
  uint8_t data[] = {0, 0, 0, 0};  // the test data
  unsigned int pdu_length;
  unsigned int bytes_used = 0;

  // with the length data set to 0, any length should fail.
  for (unsigned int i = 0; i <= sizeof(data); i++) {
    OLA_ASSERT_FALSE(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // Set the length of the pdu to 1, note that as the length includes the
  // size of the length, vector & header fields, this is less than the number
  // of bytes required to determine the length and so it fails
  data[1] = 1;
  for (unsigned int i = 0; i <= sizeof(data); i++) {
    OLA_ASSERT_FALSE(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // now set the length to 2, a data length of 0 or 1 should fail, but anything
  // more than that should return correctly.
  data[1] = 2;
  for (unsigned int i = 0; i <= 1; i++) {
    OLA_ASSERT_FALSE(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }
  for (unsigned int i = 2; i <= sizeof(data) ; i++) {
    OLA_ASSERT(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 2, pdu_length);
    OLA_ASSERT_EQ((unsigned int) 2, bytes_used);
  }

  // now check that both bytes are used
  data[0] = 1;  // total length of 258
  OLA_ASSERT(inflator.DecodeLength(data, sizeof(data), &pdu_length,
                                   &bytes_used));
  OLA_ASSERT_EQ((unsigned int) 258, pdu_length);
  OLA_ASSERT_EQ((unsigned int) 2, bytes_used);

  // now check that the extend length format works
  data[0] = BaseInflator::LFLAG_MASK;

  // with the length data set to 0, any length should fail.
  data[1] = 0;
  for (unsigned int i = 0; i <= sizeof(data); i++) {
    OLA_ASSERT_FALSE(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // Set the length of the pdu to 1, note that as the length includes the
  // size of the length, vector & header fields, this is less than the number
  // of bytes required to determine the length and so it fails
  data[2] = 1;
  for (unsigned int i = 0; i <= sizeof(data); i++) {
    OLA_ASSERT_FALSE(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }

  // now set the length to 3, a data length of 0, 1 or 2 should fail, but
  // anything more than that should return correctly.
  data[2] = 3;
  for (unsigned int i = 0; i <= 2; i++) {
    OLA_ASSERT_FALSE(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 0, bytes_used);
  }
  for (unsigned int i = 3; i <= sizeof(data) ; i++) {
    OLA_ASSERT(inflator.DecodeLength(data, i, &pdu_length, &bytes_used));
    OLA_ASSERT_EQ((unsigned int) 3, pdu_length);
    OLA_ASSERT_EQ((unsigned int) 3, bytes_used);
  }

  // now check that all 3 bytes are used
  data[0] = BaseInflator::LFLAG_MASK + 1;
  data[1] = 0x01;
  OLA_ASSERT(inflator.DecodeLength(data, sizeof(data), &pdu_length,
                                   &bytes_used));
  OLA_ASSERT_EQ((unsigned int) 65795, pdu_length);
  OLA_ASSERT_EQ((unsigned int) 3, bytes_used);
}