test/prop_tests.c

Fri, 12 Jul 2013 20:50:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Jul 2013 20:50:18 +0200
changeset 108
d2b1e67b2b48
child 109
75cb6590358b
permissions
-rw-r--r--

new properties parser

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "prop_tests.h"

UCX_TEST_IMPLEMENT(test_ucx_prop_new) {
    UcxPropParser *parser = ucx_prop_new();
    
    UCX_TEST_BEGIN
            
    UCX_TEST_ASSERT(parser != NULL, "failed");
    UCX_TEST_ASSERT(parser->buffer == NULL, "parser has buffer");
    UCX_TEST_ASSERT(parser->tmp == NULL, "parser has tmp buffer");
    
    UCX_TEST_END
            
    ucx_prop_free(parser);
}

UCX_TEST_IMPLEMENT(test_ucx_prop_parse) {  
    char *tests[] = {
        "name = value\n",
        "name=value\n",
        "n=value\n",
        "name=v\n",
        "n=v\n",
        "name = value # comment\n",
        "#comment\nn=v\n",
        "# comment1\n# comment2\n\n    \n\nname = value\n",
        "    name     =      value\n",
        "name = value\n\n"
    };
    
    char *names[] = {
        "name",
        "name",
        "n",
        "name",
        "n",
        "name",
        "n",
        "name",
        "name",
        "name"
    };
    
    char *values[] = {
        "value",
        "value",
        "value",
        "v",
        "v",
        "value",
        "v",
        "value",
        "value",
        "value"
    };
    
    UCX_TEST_BEGIN
    
    sstr_t name;
    sstr_t value;
    
    for(int i=0;i<10;i++) {
        UcxPropParser *parser = ucx_prop_new();
        
        ucx_prop_fill(parser, tests[i], strlen(tests[i]));
        UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed");
        UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen");
        
        int r = ucx_prop_parse(parser, &name, &value);
        sstr_t n = sstr(names[i]);
        sstr_t v = sstr(values[i]);
        UCX_TEST_ASSERT(r == 1, "parse returned 0");
        UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name");
        UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value");
        
        r = ucx_prop_parse(parser, &name, &value);
        UCX_TEST_ASSERT(r == 0, "parse returned 1");
        UCX_TEST_ASSERT(parser->tmp == NULL, "tmp not NULL");
        UCX_TEST_ASSERT(parser->tmpcap == 0, "tmpcap not NULL");
        UCX_TEST_ASSERT(parser->tmplen == 0, "tmplen not NULL");
        
        ucx_prop_free(parser);
    }
    
    UCX_TEST_END       
}

UCX_TEST_IMPLEMENT(test_ucx_prop_parse_multi) {
    char *names[] = {
        "a",
        "b",
        "c",
        "uap",
        "name",
        "key1",
        "key2",
        "key3"
    };
    
    char *values[] = {
        "a value",
        "b value",
        "core",
        "core",
        "ucx",
        "value1",
        "value2",
        "value3"
    };
    
    char *str = "#\n"
        "# properties\n"
        "# contains key/value pairs\n"
        "#\n"
        "a = a value\n"
        "b = b value\n"
        "c = core\n"
        "\n# test\n"
        "uap = core\n"
        "name = ucx\n"
        "# no = property\n"
        "key1 = value1\n"
        "#key1 = wrong value\n"
        "#key2 = not value 2\n"
        "key2 = value2\n"
        "\n\n\n        \n           key3=value3\n";
    
    UcxPropParser *parser = ucx_prop_new();
    
    UCX_TEST_BEGIN
    
    ucx_prop_fill(parser, str, strlen(str));
    
    sstr_t name;
    sstr_t value;
    for(int i=0;i<8;i++) {
        int r = ucx_prop_parse(parser, &name, &value);
        UCX_TEST_ASSERT(r == 1, "parse returned 0");
        UCX_TEST_ASSERT((!sstrcmp(name, sstr(names[i]))), "wrong name");
        UCX_TEST_ASSERT((!sstrcmp(value, sstr(values[i]))), "wrong value");
    }
    int r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    UCX_TEST_END
    
    ucx_prop_free(parser);
}

UCX_TEST_IMPLEMENT(test_ucx_prop_parse_part) {
    UcxPropParser *parser = ucx_prop_new();
    char *str;
    int r;
    sstr_t name;
    sstr_t value;
    
    UCX_TEST_BEGIN
    
    str = "";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "  \n";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "name";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "    ";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "= ";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "value";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "\n";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, sstr("name"))), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value");
    
    // second round
    str = "#comment\n";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "#comment\nname = ";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "value\na = b\n";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, sstr("name"))), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value");
    
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, sstr("a"))), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, sstr("b"))), "wrong value");
    
    str = "# comment\n#\n#\ntests = ";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "test1 ";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "test2 test3 test4\n";
    sstr_t testv = sstr("test1 test2 test3 test4");
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, sstr("tests"))), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value");
    
    // test if parse finds a name/value after a tmp comment
    str = "# just a comment";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = " in 3";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = " parts\na = 1\n";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, sstr("a"))), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, sstr("1"))), "wrong value");
    
    UCX_TEST_END
    
    ucx_prop_free(parser);
}

UCX_TEST_IMPLEMENT(test_ucx_prop_parse_long) {
    UcxPropParser *parser = ucx_prop_new();
    int r;
    size_t name_len = 512;
    char *long_name = (char*)malloc(name_len);
    memset(long_name, 'a', 70);
    memset(long_name+70, 'b', 242);
    memset(long_name+312, 'c', 200);
    
    size_t value_len = 2048;
    char *long_value = (char*)malloc(value_len);
    memset(long_value, 'x', 1024);
    memset(long_value+1024, 'y', 1024);
    
    UCX_TEST_BEGIN
    
    sstr_t name;
    sstr_t value;
    
    ucx_prop_fill(parser, long_name, 10);
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    ucx_prop_fill(parser, long_name+10, 202);
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    ucx_prop_fill(parser, long_name+212, 200);
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    ucx_prop_fill(parser, long_name+412, 100);
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    char *str = " = ";
    ucx_prop_fill(parser, str, strlen(str));
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    ucx_prop_fill(parser, long_value, 512);
    r = ucx_prop_parse(parser, &name, &value); 
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    ucx_prop_fill(parser, long_value+512, 1024);
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    ucx_prop_fill(parser, long_value+1536, 512);
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    str = "\n#comment\nkey = value\n";
    ucx_prop_fill(parser, str, strlen(str));    
    r = ucx_prop_parse(parser, &name, &value);
    sstr_t n = sstrn(long_name, name_len);
    sstr_t v = sstrn(long_value, value_len);
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong value");
    
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 1, "parse returned 0");
    UCX_TEST_ASSERT((!sstrcmp(name, sstr("key"))), "wrong name");
    UCX_TEST_ASSERT((!sstrcmp(value, sstr("value"))), "wrong value");
    
    r = ucx_prop_parse(parser, &name, &value);
    UCX_TEST_ASSERT(r == 0, "parse returned 1");
    
    UCX_TEST_END
    
    free(long_name);
    free(long_value);
    ucx_prop_free(parser);
}

mercurial