*((uint32_t*)userData) = 0x10000000;
}
}
+
+uint32_t UDPC_strtoa(const char *addrStr)
+{
+ uint32_t addr = 0;
+ uint32_t temp = 0;
+ uint32_t index = 0;
+ while(*addrStr != 0)
+ {
+ if(*addrStr >= '0' && *addrStr <= '9')
+ {
+ temp *= 10;
+ temp += *addrStr - '0';
+ }
+ else if(*addrStr == '.' && temp <= 0xFF && index < 3)
+ {
+ addr |= (temp << (24 - 8 * index++));
+ temp = 0;
+ }
+ else
+ {
+ return 0;
+ }
+ ++addrStr;
+ }
+
+ if(index == 3 && temp <= 0xFF)
+ {
+ addr |= temp;
+ return addr;
+ }
+ else
+ {
+ return 0;
+ }
+}
void TEST_ATOSTR()
{
- UDPC_Context *ctx = malloc(sizeof(UDPC_Context));
+ UDPC_Context ctx;
ASSERT_EQ_MEM(
- UDPC_INTERNAL_atostr(ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF),
+ UDPC_INTERNAL_atostr(&ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF),
"172.30.1.255",
13);
- free(ctx);
UNITTEST_REPORT(ATOSTR);
}
UNITTEST_REPORT(HASHMAP);
}
+void TEST_STRTOA()
+{
+ ASSERT_EQ(0x01020304, UDPC_strtoa("1.2.3.4"));
+ ASSERT_EQ(0x7F000001, UDPC_strtoa("127.0.0.1"));
+ ASSERT_EQ(0xC0A801FF, UDPC_strtoa("192.168.1.255"));
+ ASSERT_EQ(0, UDPC_strtoa("1.2.3.4.5"));
+ ASSERT_EQ(0, UDPC_strtoa("100.20.30"));
+ ASSERT_EQ(0, UDPC_strtoa("200.400.30.50"));
+ UNITTEST_REPORT(STRTOA);
+}
+
+void TEST_ATOSTRTOA()
+{
+ UDPC_Context ctx;
+
+ ASSERT_EQ(0x01020304, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0x01020304)));
+ ASSERT_EQ(0x7F000001, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0x7F000001)));
+ ASSERT_EQ(0xC0A801FF, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0xC0A801FF)));
+ ASSERT_EQ(0xFFFEFDFC, UDPC_strtoa(UDPC_INTERNAL_atostr(&ctx, 0xFFFEFDFC)));
+
+ UNITTEST_REPORT(ATOSTRTOA);
+}
+
int main()
{
TEST_DEQUE();
TEST_ATOSTR();
+ TEST_STRTOA();
+ TEST_ATOSTRTOA();
TEST_HASHMAP();
return 0;
}