test/prop_tests.c

Sun, 21 Jan 2018 14:10:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jan 2018 14:10:59 +0100
changeset 273
9c1591b3c4a4
parent 259
2f5dea574a75
permissions
-rw-r--r--

fixes return value for multiplication with zero in ucx_szmul

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "prop_tests.h"
    30 #include <ucx/mempool.h>
    32 UCX_TEST(test_ucx_properties_new) {
    33     UcxProperties *parser = ucx_properties_new();
    35     UCX_TEST_BEGIN
    37     UCX_TEST_ASSERT(parser != NULL, "failed");
    38     UCX_TEST_ASSERT(parser->buffer == NULL, "parser has buffer");
    39     UCX_TEST_ASSERT(parser->tmp == NULL, "parser has tmp buffer");
    41     UCX_TEST_END
    43     ucx_properties_free(parser);
    44 }
    46 UCX_TEST(test_ucx_properties_next) {  
    47     const char *tests[] = {
    48         "name = value\n",
    49         "name=value\n",
    50         "n=value\n",
    51         "name=v\n",
    52         "n=v\n",
    53         "name = value # comment\n",
    54         "#comment\nn=v\n",
    55         "# comment1\n# comment2\n\n    \n\nname = value\n",
    56         "    name     =      value\n",
    57         "name = value\n\n"
    58     };
    60     const char *names[] = {
    61         "name",
    62         "name",
    63         "n",
    64         "name",
    65         "n",
    66         "name",
    67         "n",
    68         "name",
    69         "name",
    70         "name"
    71     };
    73     const char *values[] = {
    74         "value",
    75         "value",
    76         "value",
    77         "v",
    78         "v",
    79         "value",
    80         "v",
    81         "value",
    82         "value",
    83         "value"
    84     };
    86     UCX_TEST_BEGIN
    88     sstr_t name;
    89     sstr_t value;
    91     for(int i=0;i<10;i++) {
    92         UcxProperties *parser = ucx_properties_new();
    94         ucx_properties_fill(parser, (char*)tests[i], strlen(tests[i]));
    95         UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed");
    96         UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen");
    98         int r = ucx_properties_next(parser, &name, &value);
    99         sstr_t n = sstr((char*)names[i]);
   100         sstr_t v = sstr((char*)values[i]);
   101         UCX_TEST_ASSERT(r == 1, "next returned 0");
   102         UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name");
   103         UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value");
   105         r = ucx_properties_next(parser, &name, &value);
   106         UCX_TEST_ASSERT(r == 0, "next returned 1");
   107         UCX_TEST_ASSERT(parser->tmp == NULL, "tmp not NULL");
   108         UCX_TEST_ASSERT(parser->tmpcap == 0, "tmpcap not NULL");
   109         UCX_TEST_ASSERT(parser->tmplen == 0, "tmplen not NULL");
   111         ucx_properties_free(parser);
   112     }
   114     UCX_TEST_END       
   115 }
   117 UCX_TEST(test_ucx_properties_next_multi) {
   118     const char *names[] = {
   119         "a",
   120         "b",
   121         "c",
   122         "uap",
   123         "name",
   124         "key1",
   125         "key2",
   126         "key3"
   127     };
   129     const char *values[] = {
   130         "a value",
   131         "b value",
   132         "core",
   133         "core",
   134         "ucx",
   135         "value1",
   136         "value2",
   137         "value3"
   138     };
   140     const char *str = "#\n"
   141         "# properties\n"
   142         "# contains key/value pairs\n"
   143         "#\n"
   144         "a = a value\n"
   145         "b = b value\n"
   146         "c = core\n"
   147         "\n# test\n"
   148         "uap = core\n"
   149         "name = ucx\n"
   150         "# no = property\n"
   151         "key1 = value1\n"
   152         "#key1 = wrong value\n"
   153         "#key2 = not value 2\n"
   154         "key2 = value2\n"
   155         "\n\n\n        \n           key3=value3\n";
   157     UcxProperties *parser = ucx_properties_new();
   159     UCX_TEST_BEGIN
   161     ucx_properties_fill(parser, (char*)str, strlen(str));
   163     sstr_t name;
   164     sstr_t value;
   165     for(int i=0;i<8;i++) {
   166         int r = ucx_properties_next(parser, &name, &value);
   167         UCX_TEST_ASSERT(r == 1, "next returned 0");
   168         UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)names[i]))), "wrong name");
   169         UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)values[i]))),
   170                 "wrong value");
   171     }
   172     int r = ucx_properties_next(parser, &name, &value);
   173     UCX_TEST_ASSERT(r == 0, "next returned 1");
   175     UCX_TEST_END
   177     ucx_properties_free(parser);
   178 }
   180 UCX_TEST(test_ucx_properties_next_part) {
   181     UcxProperties *parser = ucx_properties_new();
   182     const char *str;
   183     int r;
   184     sstr_t name;
   185     sstr_t value;
   187     UCX_TEST_BEGIN
   189     str = "";
   190     ucx_properties_fill(parser, (char*)str, strlen(str));
   191     r = ucx_properties_next(parser, &name, &value);
   192     UCX_TEST_ASSERT(r == 0, "next returned 1");
   194     str = "  \n";
   195     ucx_properties_fill(parser, (char*)str, strlen(str));
   196     r = ucx_properties_next(parser, &name, &value); 
   197     UCX_TEST_ASSERT(r == 0, "next returned 1");
   199     str = "name";
   200     ucx_properties_fill(parser, (char*)str, strlen(str));
   201     r = ucx_properties_next(parser, &name, &value); 
   202     UCX_TEST_ASSERT(r == 0, "next returned 1");
   204     str = "    ";
   205     ucx_properties_fill(parser, (char*)str, strlen(str));
   206     r = ucx_properties_next(parser, &name, &value); 
   207     UCX_TEST_ASSERT(r == 0, "next returned 1");
   209     str = "= ";
   210     ucx_properties_fill(parser, (char*)str, strlen(str));
   211     r = ucx_properties_next(parser, &name, &value); 
   212     UCX_TEST_ASSERT(r == 0, "next returned 1");
   214     str = "value";
   215     ucx_properties_fill(parser, (char*)str, strlen(str));
   216     r = ucx_properties_next(parser, &name, &value); 
   217     UCX_TEST_ASSERT(r == 0, "next returned 1");
   219     str = "\n";
   220     ucx_properties_fill(parser, (char*)str, strlen(str));
   221     r = ucx_properties_next(parser, &name, &value); 
   222     UCX_TEST_ASSERT(r == 1, "next returned 0");
   223     UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name");
   224     UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
   226     // second round
   227     str = "#comment\n";
   228     ucx_properties_fill(parser, (char*)str, strlen(str));
   229     r = ucx_properties_next(parser, &name, &value); 
   230     UCX_TEST_ASSERT(r == 0, "next returned 1");
   232     str = "#comment\nname = ";
   233     ucx_properties_fill(parser, (char*)str, strlen(str));
   234     r = ucx_properties_next(parser, &name, &value); 
   235     UCX_TEST_ASSERT(r == 0, "next returned 1");
   237     str = "value\na = b\n";
   238     ucx_properties_fill(parser, (char*)str, strlen(str));
   239     r = ucx_properties_next(parser, &name, &value); 
   240     UCX_TEST_ASSERT(r == 1, "next returned 0");
   241     UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name");
   242     UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
   244     r = ucx_properties_next(parser, &name, &value); 
   245     UCX_TEST_ASSERT(r == 1, "next returned 0");
   246     UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name");
   247     UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"b"))), "wrong value");
   249     str = "# comment\n#\n#\ntests = ";
   250     ucx_properties_fill(parser, (char*)str, strlen(str));
   251     r = ucx_properties_next(parser, &name, &value); 
   252     UCX_TEST_ASSERT(r == 0, "next returned 1");
   254     str = "test1 ";
   255     ucx_properties_fill(parser, (char*)str, strlen(str));
   256     r = ucx_properties_next(parser, &name, &value); 
   257     UCX_TEST_ASSERT(r == 0, "next returned 1");
   259     str = "test2 test3 test4\n";
   260     sstr_t testv = sstr((char*)"test1 test2 test3 test4");
   261     ucx_properties_fill(parser, (char*)str, strlen(str));
   262     r = ucx_properties_next(parser, &name, &value); 
   263     UCX_TEST_ASSERT(r == 1, "next returned 0");
   264     UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"tests"))), "wrong name");
   265     UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value");
   267     // test if ucx_properties_next finds a name/value after a tmp comment
   268     str = "# just a comment";
   269     ucx_properties_fill(parser, (char*)str, strlen(str));
   270     r = ucx_properties_next(parser, &name, &value); 
   271     UCX_TEST_ASSERT(r == 0, "next returned 1");
   273     str = " in 3";
   274     ucx_properties_fill(parser, (char*)str, strlen(str));
   275     r = ucx_properties_next(parser, &name, &value); 
   276     UCX_TEST_ASSERT(r == 0, "next returned 1");
   278     str = " parts\na = 1\n";
   279     ucx_properties_fill(parser, (char*)str, strlen(str));
   280     r = ucx_properties_next(parser, &name, &value); 
   281     UCX_TEST_ASSERT(r == 1, "next returned 0");
   282     UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name");
   283     UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"1"))), "wrong value");
   285     UCX_TEST_END
   287     ucx_properties_free(parser);
   288 }
   290 UCX_TEST(test_ucx_properties_next_long) {
   291     UcxProperties *parser = ucx_properties_new();
   292     int r;
   293     size_t name_len = 512;
   294     char *long_name = (char*)malloc(name_len);
   295     memset(long_name, 'a', 70);
   296     memset(long_name+70, 'b', 242);
   297     memset(long_name+312, 'c', 200);
   299     size_t value_len = 2048;
   300     char *long_value = (char*)malloc(value_len);
   301     memset(long_value, 'x', 1024);
   302     memset(long_value+1024, 'y', 1024);
   304     UCX_TEST_BEGIN
   306     sstr_t name;
   307     sstr_t value;
   309     ucx_properties_fill(parser, long_name, 10);
   310     r = ucx_properties_next(parser, &name, &value);
   311     UCX_TEST_ASSERT(r == 0, "next returned 1");
   313     ucx_properties_fill(parser, long_name+10, 202);
   314     r = ucx_properties_next(parser, &name, &value);
   315     UCX_TEST_ASSERT(r == 0, "next returned 1");
   317     ucx_properties_fill(parser, long_name+212, 200);
   318     r = ucx_properties_next(parser, &name, &value);
   319     UCX_TEST_ASSERT(r == 0, "next returned 1");
   321     ucx_properties_fill(parser, long_name+412, 100);
   322     r = ucx_properties_next(parser, &name, &value);
   323     UCX_TEST_ASSERT(r == 0, "next returned 1");
   325     const char *str = " = ";
   326     ucx_properties_fill(parser, (char*)str, strlen(str));
   327     r = ucx_properties_next(parser, &name, &value);
   328     UCX_TEST_ASSERT(r == 0, "next returned 1");
   330     ucx_properties_fill(parser, long_value, 512);
   331     r = ucx_properties_next(parser, &name, &value); 
   332     UCX_TEST_ASSERT(r == 0, "next returned 1");
   334     ucx_properties_fill(parser, long_value+512, 1024);
   335     r = ucx_properties_next(parser, &name, &value);
   336     UCX_TEST_ASSERT(r == 0, "next returned 1");
   338     ucx_properties_fill(parser, long_value+1536, 512);
   339     r = ucx_properties_next(parser, &name, &value);
   340     UCX_TEST_ASSERT(r == 0, "next returned 1");
   342     str = "\n#comment\nkey = value\n";
   343     ucx_properties_fill(parser, (char*)str, strlen(str));    
   344     r = ucx_properties_next(parser, &name, &value);
   345     sstr_t n = sstrn(long_name, name_len);
   346     sstr_t v = sstrn(long_value, value_len);
   347     UCX_TEST_ASSERT(r == 1, "next returned 0");
   348     UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong name");
   349     UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong value");
   351     r = ucx_properties_next(parser, &name, &value);
   352     UCX_TEST_ASSERT(r == 1, "next returned 0");
   353     UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"key"))), "wrong name");
   354     UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
   356     r = ucx_properties_next(parser, &name, &value);
   357     UCX_TEST_ASSERT(r == 0, "next returned 1");
   359     UCX_TEST_END
   361     free(long_name);
   362     free(long_value);
   363     ucx_properties_free(parser);
   364 }
   366 UCX_TEST(test_ucx_properties2map) {
   367     UcxMempool *mp = ucx_mempool_new(64);
   368     UcxMap *map = ucx_map_new_a(mp->allocator, 16);
   369     UcxProperties *parser = ucx_properties_new();
   371     UCX_TEST_BEGIN
   373     const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n";
   374     ucx_properties_fill(parser, (char*)str, strlen(str));
   376     int r = ucx_properties2map(parser, map);
   378     UCX_TEST_ASSERT(r == 0, "properties2map failed");
   379     UCX_TEST_ASSERT(map->count == 3, "wrong number of properties");
   381     char *v1 = (char*)ucx_map_cstr_get(map, "key1");
   382     char *v2 = (char*)ucx_map_cstr_get(map, "key2");
   383     char *v3 = (char*)ucx_map_cstr_get(map, "key3");
   385     UCX_TEST_ASSERT(v1, "value for key1 not found");
   386     UCX_TEST_ASSERT(v2, "value for key2 not found");
   387     UCX_TEST_ASSERT(v3, "value for key3 not found");
   389     UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1");
   390     UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2");
   391     UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3");
   393     // second test
   394     ucx_map_free(map);
   395     map = ucx_map_new_a(mp->allocator, 16);
   397     str = "\n#comment\n";
   398     ucx_properties_fill(parser, (char*)str, strlen(str));
   400     r = ucx_properties2map(parser, map);
   401     UCX_TEST_ASSERT(r == 0, "properties2map failed");
   402     UCX_TEST_ASSERT(map->count == 0, "wrong number of properties");
   404     str = "key1 = value1\nsyntax error line\n";
   405     ucx_properties_fill(parser, (char*)str, strlen(str));
   407     r = ucx_properties2map(parser, map);
   408     UCX_TEST_ASSERT(r == 1, "properties2map should return 1");
   409     UCX_TEST_ASSERT(map->count == 1, "wrong number of properties");
   411     char *v = (char*)ucx_map_cstr_get(map, "key1");
   412     UCX_TEST_ASSERT((!strcmp(v, "value1")), "wrong value");
   414     UCX_TEST_END
   416     ucx_mempool_destroy(mp);
   417     ucx_properties_free(parser);
   418 }
   420 UCX_TEST(test_ucx_properties_load) { 
   421     UCX_TEST_BEGIN
   422     FILE *f = tmpfile();
   423     UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
   425     fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
   426     fprintf(f, "\n\nkey3    = value3\n\n");
   428     size_t name_len = 512;
   429     char *long_name = (char*)malloc(name_len);
   430     memset(long_name, 'k', 512);
   432     size_t value_len = 2048;
   433     char *long_value = (char*)malloc(value_len);
   434     memset(long_value, 'v', 2048);
   436     fwrite(long_name, 1, name_len, f);
   437     fprintf(f, "    =    ");
   438     fwrite(long_value, 1, value_len, f);
   439     fprintf(f, "                         \n");
   441     fprintf(f, "\n\n\n\nlast_key = property value\n");
   443     fflush(f);
   444     fseek(f, 0, SEEK_SET);
   446     UcxMap *map = ucx_map_new(8);
   447     int r = ucx_properties_load(map, f);
   449     UCX_TEST_ASSERT(r == 0, "ucx_properties_load failed");
   450     UCX_TEST_ASSERT(map->count == 5, "wrong number of properties");
   452     char *v1 = (char*)ucx_map_cstr_get(map, "key1");
   453     char *v2 = (char*)ucx_map_cstr_get(map, "key2");
   454     char *v3 = (char*)ucx_map_cstr_get(map, "key3");
   455     char *lv = (char*)ucx_map_sstr_get(map, sstrn(long_name, name_len));
   456     char *lk = (char*)ucx_map_cstr_get(map, "last_key");
   458     UCX_TEST_ASSERT(v1, "value for key1 not found");
   459     UCX_TEST_ASSERT(v2, "value for key2 not found");
   460     UCX_TEST_ASSERT(v3, "value for key3 not found");
   461     UCX_TEST_ASSERT(lv, "value for long key not found");
   462     UCX_TEST_ASSERT(lk, "value for last_key not found");
   464     UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1");
   465     UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2");
   466     UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3");
   467     sstr_t long1 = sstrn(long_value, value_len);
   468     sstr_t long2 = sstr(lv);
   469     UCX_TEST_ASSERT((!sstrcmp(long1, long2)), "wrong value for long key");
   470     UCX_TEST_ASSERT(!strcmp(lk, "property value"), "wrong value for last_key");
   472     free(v1);
   473     free(v2);
   474     free(v3);
   475     free(lv);
   476     free(lk);
   477     ucx_map_free(map);
   478     fclose(f);
   480     free(long_name);
   481     free(long_value);
   483     UCX_TEST_END
   484 }
   486 UCX_TEST(test_ucx_properties_store) {
   487     UcxMap *map1 = ucx_map_new(16);
   488     ucx_map_cstr_put(map1, "key1", "value1");
   489     ucx_map_cstr_put(map1, "key2", "value2");
   490     ucx_map_cstr_put(map1, "key3", "value3");
   491     ucx_map_cstr_put(map1, "key4", "value4");
   492     ucx_map_cstr_put(map1, "property.key1", "some value 1");
   493     ucx_map_cstr_put(map1, "property.key2", "some value 2");
   494     ucx_map_cstr_put(map1, "property.key3", "some value 3");
   495     ucx_map_cstr_put(map1, "property.key4", "some value 4");
   497     UCX_TEST_BEGIN
   499     FILE *f = tmpfile();
   500     fprintf(f, "#\n# test property file\n#\n#\n");
   501     ucx_properties_store(map1, f);
   503     fflush(f);
   504     fseek(f, 0, SEEK_SET);
   505     UcxMap *map2 = ucx_map_new(16);
   506     ucx_properties_load(map2, f);
   508     UCX_TEST_ASSERT(map2->count == 8, "wrong number of properties in map2");
   510     char *v1 = (char*)ucx_map_cstr_get(map2, "key1");
   511     char *v2 = (char*)ucx_map_cstr_get(map2, "key2");
   512     char *v3 = (char*)ucx_map_cstr_get(map2, "key3");
   513     char *v4 = (char*)ucx_map_cstr_get(map2, "key4");
   514     char *v5 = (char*)ucx_map_cstr_get(map2, "property.key1");
   515     char *v6 = (char*)ucx_map_cstr_get(map2, "property.key2");
   516     char *v7 = (char*)ucx_map_cstr_get(map2, "property.key3");
   517     char *v8 = (char*)ucx_map_cstr_get(map2, "property.key4");
   519     UCX_TEST_ASSERT(v1 && v2 && v3 && v4 && v5 && v6 && v7 && v8,
   520             "missing values");
   522     UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value 1");
   523     UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value 2");
   524     UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value 3");
   525     UCX_TEST_ASSERT((!strcmp(v4, "value4")), "wrong value 4");
   526     UCX_TEST_ASSERT((!strcmp(v5, "some value 1")), "wrong value 5");
   527     UCX_TEST_ASSERT((!strcmp(v6, "some value 2")), "wrong value 6");
   528     UCX_TEST_ASSERT((!strcmp(v7, "some value 3")), "wrong value 7");
   529     UCX_TEST_ASSERT((!strcmp(v8, "some value 4")), "wrong value 8");
   531     free(v1);
   532     free(v2);
   533     free(v3);
   534     free(v4);
   535     free(v5);
   536     free(v6);
   537     free(v7);
   538     free(v8);
   539     ucx_map_free(map2);
   540     fclose(f);
   542     UCX_TEST_END
   544     ucx_map_free(map1);
   545 }

mercurial