mirror of
https://github.com/clearlinux/swupd-client.git
synced 2026-09-06 13:41:29 +00:00
string: Add function to convert from string to unsigned ints
Signed-off-by: Otavio Pontes <otavio.pontes@intel.com>
This commit is contained in:
@@ -239,6 +239,123 @@ static void test_str_to_int_endptr()
|
||||
|
||||
}
|
||||
|
||||
static void test_str_to_uint()
|
||||
{
|
||||
char tmp_str[100];
|
||||
int err;
|
||||
unsigned int value = 0;
|
||||
size_t i;
|
||||
|
||||
// test some standard values
|
||||
char *str_values[] = { "0", "-0", "123", "000123" };
|
||||
unsigned int values[] = { 0, 0, 123, 123 };
|
||||
|
||||
for (i = 0; i < sizeof(values) / sizeof(int); i++) {
|
||||
value = 0xffffff;
|
||||
err = str_to_uint(str_values[i], &value);
|
||||
check(err == 0);
|
||||
check(value == values[i]);
|
||||
}
|
||||
|
||||
// test limits
|
||||
value = 0xffffff;
|
||||
snprintf(tmp_str, sizeof(tmp_str), "%u", UINT_MAX - 1);
|
||||
err = str_to_uint(tmp_str, &value);
|
||||
check(err == 0);
|
||||
check(value == UINT_MAX - 1);
|
||||
|
||||
value = 0xffffff;
|
||||
snprintf(tmp_str, sizeof(tmp_str), "%u", UINT_MAX);
|
||||
err = str_to_uint(tmp_str, &value);
|
||||
check(err == 0);
|
||||
check(value == UINT_MAX);
|
||||
|
||||
// test errors
|
||||
value = 0xffffff;
|
||||
snprintf(tmp_str, sizeof(tmp_str), "%ld", (long)UINT_MAX + 1);
|
||||
err = str_to_uint(tmp_str, &value);
|
||||
check(err == -ERANGE);
|
||||
check(value == UINT_MAX);
|
||||
|
||||
value = 0xffffff;
|
||||
snprintf(tmp_str, sizeof(tmp_str), "%ld", (long)UINT_MAX + 1000);
|
||||
err = str_to_uint(tmp_str, &value);
|
||||
check(err == -ERANGE);
|
||||
check(value == UINT_MAX);
|
||||
|
||||
// Negative
|
||||
value = 0xffffff;
|
||||
snprintf(tmp_str, sizeof(tmp_str), "%d", -1);
|
||||
err = str_to_uint(tmp_str, &value);
|
||||
check(err == -ERANGE);
|
||||
check(value == 0);
|
||||
|
||||
value = 0xffffff;
|
||||
snprintf(tmp_str, sizeof(tmp_str), "%d", INT_MIN);
|
||||
err = str_to_uint(tmp_str, &value);
|
||||
check(err == -ERANGE);
|
||||
check(value == 0);
|
||||
|
||||
// test EINVAL errors
|
||||
char *einval_errors[] = { "string", "123.456", "123a", "a123a", "456b123", "0x12345", "4f"};
|
||||
|
||||
for (i = 0; i < sizeof(einval_errors) / sizeof(char *); i++) {
|
||||
err = str_to_uint(einval_errors[i], &value);
|
||||
check(err == -EINVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_str_to_uint_endptr()
|
||||
{
|
||||
char *endptr;
|
||||
int err;
|
||||
unsigned int value = 0;
|
||||
|
||||
// Test endptr trailing chars
|
||||
value = 0xffffff;
|
||||
endptr = NULL;
|
||||
err = str_to_uint_endptr("123.456", &endptr, &value);
|
||||
check(err == 0);
|
||||
check(value == 123);
|
||||
check(endptr != NULL);
|
||||
check(*endptr == '.');
|
||||
|
||||
value = 0xffffff;
|
||||
endptr = NULL;
|
||||
err = str_to_uint_endptr("123a456", &endptr, &value);
|
||||
check(err == 0);
|
||||
check(value == 123);
|
||||
check(endptr != NULL);
|
||||
check(*endptr == 'a');
|
||||
|
||||
value = 0xffffff;
|
||||
endptr = NULL;
|
||||
err = str_to_uint_endptr("123testing456", &endptr, &value);
|
||||
check(err == 0);
|
||||
check(value == 123);
|
||||
check(endptr != NULL);
|
||||
check(*endptr == 't');
|
||||
|
||||
value = 0xffffff;
|
||||
endptr = NULL;
|
||||
err = str_to_uint_endptr("123-456", &endptr, &value);
|
||||
check(err == 0);
|
||||
check(value == 123);
|
||||
check(endptr != NULL);
|
||||
check(*endptr == '-');
|
||||
|
||||
value = 0xffffff;
|
||||
endptr = NULL;
|
||||
err = str_to_uint_endptr("-123.456", &endptr, &value);
|
||||
check(err == -ERANGE);
|
||||
|
||||
value = 0xffffff;
|
||||
endptr = NULL;
|
||||
err = str_to_uint_endptr("-123a456", &endptr, &value);
|
||||
check(err == -ERANGE);
|
||||
|
||||
}
|
||||
|
||||
void test_str_join()
|
||||
{
|
||||
struct list *str_list = NULL;
|
||||
@@ -280,6 +397,8 @@ int main() {
|
||||
test_str_to_int();
|
||||
test_str_to_int_endptr();
|
||||
test_str_join();
|
||||
test_str_to_uint();
|
||||
test_str_to_uint_endptr();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user