Ejemplo n.º 1
0
END_TEST


static bool test_reported_force(Streader* sr, double expected)
{
    assert(sr != NULL);

    double actual = NAN;

    if (!(Streader_readf(sr, "[[0, [") &&
                Streader_match_string(sr, "qf") &&
                Streader_readf(sr, ", null]], [0, [") &&
                Streader_match_string(sr, "Af") &&
                Streader_readf(sr, ", %f]]]", &actual)))
        return false;

    if (fabs(actual - expected) > 0.1)
    {
        Streader_set_error(
                sr, "Expected force %.4f, got %.4f", expected, actual);
        return false;
    }

    return true;
}
Ejemplo n.º 2
0
END_TEST


START_TEST(Matching_wrong_strings_fails)
{
    Streader* sr = init_with_cstr("\"\"");
    fail_if(Streader_match_string(sr, " "),
            "Empty string and string with whitespace were matched");

    sr = init_with_cstr("\" \"");
    fail_if(Streader_match_string(sr, ""),
            "Empty string and string with whitespace were matched");
}
Ejemplo n.º 3
0
bool read_received_events(Streader* sr, int32_t index, void* userdata)
{
    assert(sr != NULL);
    (void)index;
    assert(userdata != NULL);

    int32_t* expected = userdata;
    double actual = NAN;

    const char* event_name = (*expected % 16 == 0) ? "n+" : "vs";

    if (!(Streader_readf(sr, "[0, [") &&
                Streader_match_string(sr, event_name) &&
                Streader_readf(sr, ", %f]]", &actual))
       )
        return false;

    if ((int)round(actual) != *expected)
    {
        Streader_set_error(
                sr,
                "Received argument %" PRId64 " instead of %" PRId32,
                actual, *expected);
        return false;
    }

    *expected = (int)round(actual) + 1;

    return true;
}
Ejemplo n.º 4
0
END_TEST


START_TEST(Matching_strings_requires_quotes_in_data)
{
    Streader* sr = init_with_cstr("abc");
    fail_if(Streader_match_string(sr, "abc"),
            "Matched a string without double quotes in data");

    sr = init_with_cstr("abc\"");
    fail_if(Streader_match_string(sr, "abc"),
            "Matched a string without opening double quote in data");

    sr = init_with_cstr("\"abc");
    fail_if(Streader_match_string(sr, "abc"),
            "Matched a string without closing double quote in data");
}
Ejemplo n.º 5
0
bool read_received_events_bind(Streader* sr, int32_t index, void* userdata)
{
    assert(sr != NULL);
    (void)index;
    assert(userdata != NULL);

    int32_t* expected = userdata;
    double actual = NAN;

    if (index == 0 && *expected == 0)
    {
        return Streader_readf(sr, "[0, [") &&
            Streader_match_string(sr, "#") &&
            Streader_match_char(sr, ',') &&
            Streader_read_string(sr, 0, NULL) &&
            Streader_readf(sr, "]]");
    }

    if (!(Streader_readf(sr, "[0, [") &&
                Streader_match_string(sr, "n+") &&
                Streader_readf(sr, ", %f]]", &actual))
       )
        return false;

    if ((int)round(actual) != *expected)
    {
        Streader_set_error(
                sr,
                "Received argument %.0f instead of %" PRId32,
                actual, *expected);
        return false;
    }

    *expected = (int)round(actual) + 1;

    return true;
}
Ejemplo n.º 6
0
END_TEST


START_TEST(Matching_strings_succeeds)
{
    Streader* sr = init_with_cstr("\"\"");
    fail_if(!Streader_match_string(sr, ""), "Could not match empty string");

    sr = init_with_cstr("\" \"");
    fail_if(!Streader_match_string(sr, " "),
            "Could not match a string with whitespace");

    sr = init_with_cstr("\"abc\"");
    fail_if(!Streader_match_string(sr, "abc"),
            "Could not match the string \"abc\"");

    sr = init_with_cstr("\"\" \"\"");
    fail_if(!Streader_match_string(sr, ""),
            "Could not match the first of two empty strings");
    fail_if(!Streader_match_string(sr, ""),
            "Could not match the second of two empty strings");
    fail_if(Streader_match_string(sr, ""),
            "Matched an empty string when end of data should have been reached");
}