Blob Blame History Raw
# -*- Autotest -*-

AT_BANNER([xfuncs])

## --------------- ##
## xstrdup_between ##
## --------------- ##

AT_TESTFUN([xstrdup_between],
[[#include "internal_libreport.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>

#define DEBUG_PRINT(e, r, o, c, s) \
        fprintf(stderr, "Expected: '%s'\nFound:    '%s'\nOpen:     '%s'\nClose:    '%s'\nSource:   '%s'\n", e, r, o, c, s)

void test(const char *src, const char *open, const char *close, const char *exp)
{
    char *res = xstrdup_between(src, open, close);

    if (exp == NULL && res != NULL)
    {
        DEBUG_PRINT("NULL", res, open, close, src);
        assert(!"Found non-existing section.");
    }
    else if (exp != NULL && res == NULL)
    {
        DEBUG_PRINT(exp, "NULL", open, close, src);
        assert(!"Didn't find section.");
    }
    else if (exp == NULL && res == NULL)
        return;
    else if (strcmp(res, exp) != 0)
    {
        DEBUG_PRINT(exp, res, open, close, src);
        assert(!"Invalid selection.");
    }

    free(res);
}


int main(void)
{
    g_verbose=3;

    test("<a>foo blah</a>", "<?>", "</a>", NULL);
    test("<a>foo blah</a>", "<a>", "</?>", NULL);
    test("<a></a>", "<a>", "</a>", "");
    test("@.$.@GOOD.$.", "@.$.@", ".$.", "GOOD");

    return 0;
}

]])

## ------------- ##
## parse_numbers ##
## ------------- ##

AT_TESTFUN([parse_numbers],
[[#include "internal_libreport.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
    g_verbose=3;

    /* 1. not a number - ERROR
     * 2. a number with an alpha suffix - ERROR
     * 3. out of range number - ERROR
     *    a. max + 1 - ERROR
     *    b. min - 1 - ERROR
     *    c. max - OK
     *    d. min - OK
     */

    {
        unsigned uint_value = 12345;
        assert(try_atou("foo", &uint_value) != 0);
        assert(uint_value == 12345);

        assert(try_atou("foo54321", &uint_value) != 0);
        assert(uint_value == 12345);

        char buf[sizeof(unsigned long long) * 3 + 1];

        snprintf(buf, sizeof(buf), "%llu", 1LLU + UINT_MAX);
        assert(try_atou(buf, &uint_value) != 0 || !"Above UINT_MAX");
        assert(uint_value == 12345);

        assert(try_atou("-1", &uint_value) != 0);
        assert(uint_value == 12345);

        snprintf(buf, sizeof(buf), "%llu", (unsigned long long)UINT_MAX);
        assert(try_atou(buf, &uint_value) == 0);
        assert(uint_value == UINT_MAX);
        assert(xatou(buf) == UINT_MAX);

        assert(try_atou("0", &uint_value) == 0);
        assert(uint_value == 0);
        assert(xatou("0") == 0);
    }

    {
        int int_value = 12345;
        assert(try_atoi("foo", &int_value) != 0);
        assert(int_value == 12345);

        assert(try_atoi("foo54321", &int_value) != 0);
        assert(int_value == 12345);

        char buf[sizeof(long long) * 3 + 1];

        snprintf(buf, sizeof(buf), "%lld", 1LL + INT_MAX);
        assert(try_atoi(buf, &int_value) != 0 || !"Parse INT_MAX+1");
        assert(int_value == 12345 || !"Above INT_MAX");

        snprintf(buf, sizeof(buf), "%lld", -1LL + INT_MIN);
        assert(try_atoi(buf, &int_value) != 0 || !"Parse INT_MIN-1");
        assert(int_value == 12345 || !"Belove INT_MIN");

        snprintf(buf, sizeof(buf), "%lld", (unsigned long long)INT_MAX);
        assert(try_atoi(buf, &int_value) == 0 || !"Parse INT_MAX");
        assert(int_value == INT_MAX);
        assert(xatoi(buf) == INT_MAX);

        snprintf(buf, sizeof(buf), "%lld", (unsigned long long)INT_MIN);
        assert(try_atoi(buf, &int_value) == 0 || !"Parse INT_MIN");
        assert(int_value == INT_MIN);
        assert(xatoi(buf) == INT_MIN);
    }

    {
        int positive_value = 12345;
        assert(try_atoi_positive("foo", &positive_value) != 0);
        assert(positive_value == 12345);

        assert(try_atoi_positive("foo54321", &positive_value) != 0);
        assert(positive_value == 12345);

        char buf[sizeof(long long) * 3 + 1];

        snprintf(buf, sizeof(buf), "%lld", 1LL + INT_MAX);
        assert(try_atoi_positive(buf, &positive_value) != 0);
        assert(positive_value == 12345 || !"Above INT_MAX");

        assert(try_atoi_positive("-1", &positive_value) != 0);
        assert(positive_value == 12345 || !"After -1");

        snprintf(buf, sizeof(buf), "%lld", (unsigned long long)INT_MAX);
        assert(try_atoi_positive(buf, &positive_value) == 0 || !"Parse INT_MAX");
        assert(positive_value == INT_MAX);
        assert(xatoi_positive(buf) == INT_MAX);

        assert(try_atoi_positive("0", &positive_value) == 0);
        assert(positive_value == 0);
        assert(xatoi_positive("0") == 0);
    }

    return 0;
}
]])