Example #1
0
void StringUtilsTest::testHexStringToInt() {
  unsigned int value;
  OLA_ASSERT_FALSE(HexStringToInt("", &value));
  OLA_ASSERT_FALSE(HexStringToInt("-1", &value));

  OLA_ASSERT_TRUE(HexStringToInt("0", &value));
  OLA_ASSERT_EQ(0u, value);
  OLA_ASSERT_TRUE(HexStringToInt("1", &value));
  OLA_ASSERT_EQ(1u, value);
  OLA_ASSERT_TRUE(HexStringToInt("a", &value));
  OLA_ASSERT_EQ(10u, value);
  OLA_ASSERT_TRUE(HexStringToInt("f", &value));
  OLA_ASSERT_EQ(15u, value);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value));
  OLA_ASSERT_EQ(161u, value);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value));
  OLA_ASSERT_EQ(255u, value);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value));
  OLA_ASSERT_EQ(161u, value);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value));
  OLA_ASSERT_EQ(255u, value);
  OLA_ASSERT_TRUE(HexStringToInt("ffff", &value));
  OLA_ASSERT_EQ(65535u, value);

  OLA_ASSERT_TRUE(HexStringToInt("ffffff", &value));
  OLA_ASSERT_EQ((unsigned int) 16777215, value);
  OLA_ASSERT_TRUE(HexStringToInt("ffffffff", &value));
  OLA_ASSERT_EQ((unsigned int) 4294967295UL, value);
  OLA_ASSERT_TRUE(HexStringToInt("ef123456", &value));
  OLA_ASSERT_EQ((unsigned int) 4010947670UL, value);
  OLA_ASSERT_FALSE(HexStringToInt("fz", &value));
  OLA_ASSERT_FALSE(HexStringToInt("zfff", &value));
  OLA_ASSERT_FALSE(HexStringToInt("0xf", &value));

  // uint16_t
  uint16_t value2;
  OLA_ASSERT_FALSE(HexStringToInt("", &value2));
  OLA_ASSERT_FALSE(HexStringToInt("-1", &value2));

  OLA_ASSERT_TRUE(HexStringToInt("0", &value2));
  OLA_ASSERT_EQ((uint16_t) 0, value2);
  OLA_ASSERT_TRUE(HexStringToInt("1", &value2));
  OLA_ASSERT_EQ((uint16_t) 1, value2);
  OLA_ASSERT_TRUE(HexStringToInt("a", &value2));
  OLA_ASSERT_EQ((uint16_t) 10, value2);
  OLA_ASSERT_TRUE(HexStringToInt("f", &value2));
  OLA_ASSERT_EQ((uint16_t) 15, value2);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value2));
  OLA_ASSERT_EQ((uint16_t) 161, value2);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value2));
  OLA_ASSERT_EQ((uint16_t) 255, value2);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value2));
  OLA_ASSERT_EQ((uint16_t) 161, value2);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value2));
  OLA_ASSERT_EQ((uint16_t) 255, value2);
  OLA_ASSERT_TRUE(HexStringToInt("400", &value2));
  OLA_ASSERT_EQ((uint16_t) 1024, value2);
  OLA_ASSERT_TRUE(HexStringToInt("ffff", &value2));
  OLA_ASSERT_EQ((uint16_t) 65535, value2);

  OLA_ASSERT_FALSE(HexStringToInt("ffffff", &value2));
  OLA_ASSERT_FALSE(HexStringToInt("ffffffff", &value2));
  OLA_ASSERT_FALSE(HexStringToInt("ef123456", &value2));
  OLA_ASSERT_FALSE(HexStringToInt("fz", &value2));
  OLA_ASSERT_FALSE(HexStringToInt("zfff", &value2));
  OLA_ASSERT_FALSE(HexStringToInt("0xf", &value2));

  // int8_t
  int8_t value3;
  OLA_ASSERT_FALSE(HexStringToInt("", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("-1", &value3));

  OLA_ASSERT_TRUE(HexStringToInt("0", &value3));
  OLA_ASSERT_EQ((int8_t) 0, value3);
  OLA_ASSERT_TRUE(HexStringToInt("1", &value3));
  OLA_ASSERT_EQ((int8_t) 1, value3);
  OLA_ASSERT_TRUE(HexStringToInt("a", &value3));
  OLA_ASSERT_EQ((int8_t) 10, value3);
  OLA_ASSERT_TRUE(HexStringToInt("f", &value3));
  OLA_ASSERT_EQ((int8_t) 15, value3);
  OLA_ASSERT_TRUE(HexStringToInt("7f", &value3));
  OLA_ASSERT_EQ((int8_t) 127, value3);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value3));
  OLA_ASSERT_EQ((int8_t) -95, value3);
  OLA_ASSERT_TRUE(HexStringToInt("80", &value3));
  OLA_ASSERT_EQ((int8_t) -128, value3);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value3));
  OLA_ASSERT_EQ((int8_t) -1, value3);

  OLA_ASSERT_FALSE(HexStringToInt("ffff", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("fff0", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("ffffff", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("ffffffff", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("ef123456", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("fz", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("zfff", &value3));
  OLA_ASSERT_FALSE(HexStringToInt("0xf", &value3));

  // int16_t
  int16_t value4;
  OLA_ASSERT_FALSE(HexStringToInt("", &value4));
  OLA_ASSERT_FALSE(HexStringToInt("-1", &value4));

  OLA_ASSERT_TRUE(HexStringToInt("0", &value4));
  OLA_ASSERT_EQ((int16_t) 0, value4);
  OLA_ASSERT_TRUE(HexStringToInt("1", &value4));
  OLA_ASSERT_EQ((int16_t) 1, value4);
  OLA_ASSERT_TRUE(HexStringToInt("a", &value4));
  OLA_ASSERT_EQ((int16_t) 10, value4);
  OLA_ASSERT_TRUE(HexStringToInt("f", &value4));
  OLA_ASSERT_EQ((int16_t) 15, value4);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value4));
  OLA_ASSERT_EQ((int16_t) 161, value4);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value4));
  OLA_ASSERT_EQ((int16_t) 255, value4);
  OLA_ASSERT_TRUE(HexStringToInt("7fff", &value4));
  OLA_ASSERT_EQ((int16_t) 32767, value4);
  OLA_ASSERT_TRUE(HexStringToInt("ffff", &value4));
  OLA_ASSERT_EQ((int16_t) -1, value4);
  OLA_ASSERT_TRUE(HexStringToInt("fff0", &value4));
  OLA_ASSERT_EQ((int16_t) -16, value4);
  OLA_ASSERT_TRUE(HexStringToInt("8000", &value4));
  OLA_ASSERT_EQ((int16_t) -32768, value4);

  OLA_ASSERT_FALSE(HexStringToInt("ffffff", &value4));
  OLA_ASSERT_FALSE(HexStringToInt("ffffffff", &value4));
  OLA_ASSERT_FALSE(HexStringToInt("ef123456", &value4));
  OLA_ASSERT_FALSE(HexStringToInt("fz", &value4));
  OLA_ASSERT_FALSE(HexStringToInt("zfff", &value4));
  OLA_ASSERT_FALSE(HexStringToInt("0xf", &value4));

  // int32
  int32_t value5;
  OLA_ASSERT_FALSE(HexStringToInt("", &value5));
  OLA_ASSERT_FALSE(HexStringToInt("-1", &value5));

  OLA_ASSERT_TRUE(HexStringToInt("0", &value5));
  OLA_ASSERT_EQ((int32_t) 0, value5);
  OLA_ASSERT_TRUE(HexStringToInt("1", &value5));
  OLA_ASSERT_EQ((int32_t) 1, value5);
  OLA_ASSERT_TRUE(HexStringToInt("a", &value5));
  OLA_ASSERT_EQ((int32_t) 10, value5);
  OLA_ASSERT_TRUE(HexStringToInt("f", &value5));
  OLA_ASSERT_EQ((int32_t) 15, value5);
  OLA_ASSERT_TRUE(HexStringToInt("a1", &value5));
  OLA_ASSERT_EQ((int32_t) 161, value5);
  OLA_ASSERT_TRUE(HexStringToInt("ff", &value5));
  OLA_ASSERT_EQ((int32_t) 255, value5);
  OLA_ASSERT_TRUE(HexStringToInt("7fff", &value5));
  OLA_ASSERT_EQ((int32_t) 32767, value5);
  OLA_ASSERT_TRUE(HexStringToInt("ffff", &value5));
  OLA_ASSERT_EQ((int32_t) 65535, value5);
  OLA_ASSERT_TRUE(HexStringToInt("ffffffff", &value5));
  OLA_ASSERT_EQ((int32_t) -1, value5);
  OLA_ASSERT_TRUE(HexStringToInt("fffffff0", &value5));
  OLA_ASSERT_EQ((int32_t) -16, value5);
  OLA_ASSERT_TRUE(HexStringToInt("80000000", &value5));
  OLA_ASSERT_EQ((int32_t) -2147483647 - 1, value5);
}