"Fossies" - the Fresh Open Source Software Archive

Member "udunits-2.2.28/lib/testUnits-1.c" (7 Dec 2020, 12574 Bytes) of package /linux/privat/udunits-2.2.28.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "testUnits-1.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.2.26_vs_2.2.28.

    1 /*
    2  * Copyright 2020 University Corporation for Atmospheric Research
    3  *
    4  * This file is part of the UDUNITS-2 package.  See the file COPYRIGHT
    5  * in the top-level source-directory of the package for copying and
    6  * redistribution conditions.
    7  */
    8 #include "config.h"
    9 
   10 #include "udunits.h"
   11 
   12 #include <float.h>
   13 #include <glob.h>
   14 #include <math.h>
   15 #include <stdarg.h>
   16 #include <stdlib.h>
   17 #include <stdio.h>
   18 #include <unistd.h>
   19 #include <CUnit/CUnit.h>
   20 #include <CUnit/Basic.h>
   21 
   22 
   23 static utUnit       kilogram;
   24 static utUnit       watt;
   25 
   26 
   27 static int
   28 setup(
   29     void)
   30 {
   31     return utInit("/foo/bar");
   32 }
   33 
   34 
   35 static int
   36 teardown(
   37     void)
   38 {
   39     utTerm();
   40     return 0;
   41 }
   42 
   43 
   44 static void
   45 test_utIsInit(void)
   46 {
   47     CU_ASSERT_TRUE(utIsInit());
   48 }
   49 
   50 
   51 static void
   52 test_utScan(void)
   53 {
   54     utUnit  unit;
   55 
   56     utIni(&kilogram);
   57     CU_ASSERT_EQUAL(utScan("kg", &kilogram), 0);
   58     utIni(&watt);
   59     CU_ASSERT_EQUAL(utScan("WATT", &watt), 0);
   60     utIni(&unit);
   61     CU_ASSERT_EQUAL(utScan("seconds since 1994-12-15 12:30:00 10", &unit), 0);
   62     CU_ASSERT_EQUAL(utScan("Celsius @ 100", &unit), 0);
   63     CU_ASSERT_EQUAL(utScan("34 quatloos", &unit), UT_EUNKNOWN);
   64     CU_ASSERT_EQUAL(utScan("$&/^", &unit), UT_EUNKNOWN);
   65     CU_ASSERT_EQUAL(utScan(NULL, &unit), UT_EINVALID);
   66     CU_ASSERT_EQUAL(utScan("kg", NULL), UT_EINVALID);
   67 }
   68 
   69 
   70 static void
   71 test_utCalendar(void)
   72 {
   73     utUnit  unit;
   74     int     year, month, day, hour, minute;
   75     float   second;
   76 
   77     utIni(&unit);
   78     CU_ASSERT_EQUAL(utScan("seconds since 1994-12-15 2:29:60.0000 UTC", &unit),
   79     0);
   80     CU_ASSERT_EQUAL(
   81     utCalendar(1, &unit, &year, &month, &day, &hour, &minute, &second), 0);
   82     CU_ASSERT_EQUAL(year, 1994);
   83     CU_ASSERT_EQUAL(month, 12);
   84     CU_ASSERT_EQUAL(day, 15);
   85     CU_ASSERT_EQUAL(hour, 2);
   86     CU_ASSERT_EQUAL(minute, 30);
   87     CU_ASSERT_EQUAL(second, 1);
   88     CU_ASSERT_EQUAL(utScan("seconds since 1925-01-01 00:00:00", &unit), 0);
   89     CU_ASSERT_EQUAL(
   90     utCalendar(1, &unit, &year, &month, &day, &hour, &minute, &second), 0);
   91     CU_ASSERT_EQUAL(year, 1925);
   92     CU_ASSERT_EQUAL(month, 1);
   93     CU_ASSERT_EQUAL(day, 1);
   94     CU_ASSERT_EQUAL(hour, 0);
   95     CU_ASSERT_EQUAL(minute, 0);
   96     CU_ASSERT_EQUAL(second, 1);
   97     utFree(&unit);
   98 
   99     utIni(&unit);
  100     CU_ASSERT_EQUAL(utScan("hours since 1901-01-01 00:00:00", &unit), 0);
  101     (void)utCalendar(876595, &unit, &year, &month, &day, &hour, &minute,
  102             &second);
  103     (void)printf("  Decoded date and time: %d-%02d-%02d %02d:%02d:%.7g UTC\n",
  104             year, month, day, hour, minute, second);
  105     CU_ASSERT_EQUAL(year, 2000);
  106     CU_ASSERT_EQUAL(month, 12);
  107     CU_ASSERT_EQUAL(day, 31);
  108     CU_ASSERT_EQUAL(hour, 19);
  109     CU_ASSERT_EQUAL(minute, 0);
  110     utFree(&unit);
  111 }
  112 
  113 
  114 static void
  115 test_utInvCalendar(void)
  116 {
  117     utUnit  unit;
  118     double  value;
  119 
  120     utIni(&unit);
  121     CU_ASSERT_EQUAL(utScan("seconds since 1994-12-15 2:29:60.0000 UTC", &unit),
  122     0);
  123     CU_ASSERT_EQUAL(utInvCalendar(1994, 12, 15, 2, 30, 1.0, &unit, &value), 0);
  124     CU_ASSERT_EQUAL(value, 1);
  125     CU_ASSERT_EQUAL(utScan("seconds since 1925-01-01 00:00:00", &unit), 0);
  126     CU_ASSERT_EQUAL(utInvCalendar(1925, 1, 1, 0, 0, 1.0, &unit, &value), 0);
  127     CU_ASSERT_EQUAL(value, 1);
  128 }
  129 
  130 
  131 static void
  132 test_utIsTime(void)
  133 {
  134     utUnit  unit;
  135 
  136     utIni(&unit);
  137     CU_ASSERT_EQUAL(utScan("seconds", &unit), 0);
  138     CU_ASSERT_TRUE(utIsTime(&unit));
  139     CU_ASSERT_EQUAL(utScan("seconds since 1925-01-01 00:00:00", &unit), 0);
  140     CU_ASSERT_TRUE(utIsTime(&unit));
  141     CU_ASSERT_EQUAL(utScan("kg", &unit), 0);
  142     CU_ASSERT_FALSE(utIsTime(&unit));
  143     CU_ASSERT_EQUAL(utScan("watt", &unit), 0);
  144     CU_ASSERT_FALSE(utIsTime(&unit));
  145     CU_ASSERT_EQUAL(utScan("celsius", &unit), 0);
  146     CU_ASSERT_FALSE(utIsTime(&unit));
  147 }
  148 
  149 
  150 static void
  151 test_utHasOrigin(void)
  152 {
  153     utUnit  unit;
  154 
  155     utIni(&unit);
  156     CU_ASSERT_EQUAL(utScan("seconds", &unit), 0);
  157     CU_ASSERT_FALSE(utHasOrigin(&unit));
  158     CU_ASSERT_EQUAL(utScan("seconds since 1925-01-01 00:00:00", &unit), 0);
  159     CU_ASSERT_TRUE(utHasOrigin(&unit));
  160     CU_ASSERT_EQUAL(utScan("kg", &unit), 0);
  161     CU_ASSERT_FALSE(utHasOrigin(&unit));
  162     CU_ASSERT_EQUAL(utScan("watt", &unit), 0);
  163     CU_ASSERT_FALSE(utHasOrigin(&unit));
  164     CU_ASSERT_EQUAL(utScan("celsius", &unit), 0);
  165     CU_ASSERT_TRUE(utHasOrigin(&unit));
  166 }
  167 
  168 
  169 static void
  170 test_utClear(void)
  171 {
  172     utUnit  one;
  173     utUnit  unit;
  174     double  slope, intercept;
  175 
  176     utIni(&one);
  177     utIni(&unit);
  178     CU_ASSERT_EQUAL(utScan("1", &one), 0);
  179     CU_ASSERT_EQUAL(utScan("seconds", &unit), 0);
  180     CU_ASSERT_EQUAL(utConvert(&one, &unit, &slope, &intercept), UT_ECONVERT);
  181     CU_ASSERT_PTR_NOT_NULL(utClear(&unit));
  182     CU_ASSERT_EQUAL(utConvert(&one, &unit, &slope, &intercept), 0);
  183     CU_ASSERT_EQUAL(slope, 1);
  184     CU_ASSERT_EQUAL(intercept, 0);
  185     CU_ASSERT_PTR_NULL(utClear(NULL));
  186 }
  187 
  188 
  189 static void
  190 test_utCopy(void)
  191 {
  192     utUnit  unit1;
  193     utUnit  unit2;
  194     double  slope, intercept;
  195 
  196     utIni(&unit1);
  197     utIni(&unit2);
  198     CU_ASSERT_EQUAL(utScan("seconds", &unit1), 0);
  199     CU_ASSERT_PTR_NOT_NULL(utCopy(&unit1, &unit2));
  200     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  201     CU_ASSERT_EQUAL(slope, 1);
  202     CU_ASSERT_EQUAL(intercept, 0);
  203     CU_ASSERT_PTR_NULL(utCopy(NULL, &unit2));
  204     CU_ASSERT_PTR_NULL(utCopy(&unit1, NULL));
  205 }
  206 
  207 
  208 static void
  209 test_utMultiply(void)
  210 {
  211     utUnit  m, s, result, expect;
  212     double  slope, intercept;
  213 
  214     utIni(&m);
  215     utIni(&s);
  216     utIni(&result);
  217     utIni(&expect);
  218     CU_ASSERT_EQUAL(utScan("m", &m), 0);
  219     CU_ASSERT_EQUAL(utScan("s", &s), 0);
  220     CU_ASSERT_PTR_NOT_NULL(utMultiply(&m, &s, &result));
  221     CU_ASSERT_EQUAL(utScan("m.s", &expect), 0);
  222     CU_ASSERT_EQUAL(utConvert(&result, &expect, &slope, &intercept), 0);
  223     CU_ASSERT_EQUAL(slope, 1);
  224     CU_ASSERT_EQUAL(intercept, 0);
  225     CU_ASSERT_PTR_NULL(utMultiply(NULL, &s, &result));
  226     CU_ASSERT_PTR_NULL(utMultiply(&m, NULL, &result));
  227 }
  228 
  229 
  230 static void
  231 test_utDivide(void)
  232 {
  233     utUnit  m, s, result, expect;
  234     double  slope, intercept;
  235 
  236     utIni(&m);
  237     utIni(&s);
  238     utIni(&result);
  239     utIni(&expect);
  240     CU_ASSERT_EQUAL(utScan("m", &m), 0);
  241     CU_ASSERT_EQUAL(utScan("s", &s), 0);
  242     CU_ASSERT_PTR_NOT_NULL(utDivide(&m, &s, &result));
  243     CU_ASSERT_EQUAL(utScan("m/s", &expect), 0);
  244     CU_ASSERT_EQUAL(utConvert(&result, &expect, &slope, &intercept), 0);
  245     CU_ASSERT_EQUAL(slope, 1);
  246     CU_ASSERT_EQUAL(intercept, 0);
  247     CU_ASSERT_PTR_NULL(utDivide(NULL, &s, &result));
  248     CU_ASSERT_PTR_NULL(utDivide(&m, NULL, &result));
  249 }
  250 
  251 
  252 static void
  253 test_utInvert(void)
  254 {
  255     utUnit  unit, result, expect;
  256     double  slope, intercept;
  257 
  258     utIni(&unit);
  259     utIni(&result);
  260     utIni(&expect);
  261     CU_ASSERT_EQUAL(utScan("m", &unit), 0);
  262     CU_ASSERT_PTR_NOT_NULL(utInvert(&unit, &result));
  263     CU_ASSERT_EQUAL(utScan("1/m", &expect), 0);
  264     CU_ASSERT_EQUAL(utConvert(&result, &expect, &slope, &intercept), 0);
  265     CU_ASSERT_EQUAL(slope, 1);
  266     CU_ASSERT_EQUAL(intercept, 0);
  267     CU_ASSERT_PTR_NULL(utInvert(NULL, &result));
  268 }
  269 
  270 
  271 static void
  272 test_utRaise(void)
  273 {
  274     utUnit  unit, result, expect;
  275     double  slope, intercept;
  276 
  277     utIni(&unit);
  278     utIni(&result);
  279     utIni(&expect);
  280     CU_ASSERT_EQUAL(utScan("watt", &unit), 0);
  281     CU_ASSERT_PTR_NOT_NULL(utRaise(&unit, 2, &result));
  282     CU_ASSERT_EQUAL(utScan("watt^2", &expect), 0);
  283     CU_ASSERT_EQUAL(utConvert(&result, &expect, &slope, &intercept), 0);
  284     CU_ASSERT_EQUAL(slope, 1);
  285     CU_ASSERT_EQUAL(intercept, 0);
  286     CU_ASSERT_PTR_NULL(utRaise(NULL, 2, &result));
  287 }
  288 
  289 
  290 static void
  291 test_utShift(void)
  292 {
  293     utUnit  unit, result, expect;
  294     double  slope, intercept;
  295 
  296     utIni(&unit);
  297     utIni(&result);
  298     utIni(&expect);
  299     CU_ASSERT_EQUAL(utScan("kelvin", &unit), 0);
  300     CU_ASSERT_PTR_NOT_NULL(utShift(&unit, 273.15, &result));
  301     CU_ASSERT_EQUAL(utScan("celsius", &expect), 0);
  302     CU_ASSERT_EQUAL(utConvert(&result, &expect, &slope, &intercept), 0);
  303     CU_ASSERT_EQUAL(slope, 1);
  304     CU_ASSERT_EQUAL(intercept, 0);
  305     CU_ASSERT_PTR_NULL(utShift(NULL, 2, &result));
  306 }
  307 
  308 
  309 static void
  310 test_utScale(void)
  311 {
  312     utUnit  unit, result, expect;
  313     double  slope, intercept;
  314 
  315     utIni(&unit);
  316     utIni(&result);
  317     utIni(&expect);
  318     CU_ASSERT_EQUAL(utScan("meter", &unit), 0);
  319     CU_ASSERT_PTR_NOT_NULL(utScale(&unit, 1000, &result));
  320     CU_ASSERT_EQUAL(utScan("kilometer", &expect), 0);
  321     CU_ASSERT_EQUAL(utConvert(&result, &expect, &slope, &intercept), 0);
  322     CU_ASSERT_EQUAL(slope, 1);
  323     CU_ASSERT_EQUAL(intercept, 0);
  324     CU_ASSERT_PTR_NULL(utScale(NULL, 2, &result));
  325 }
  326 
  327 
  328 static void
  329 test_utPrint(void)
  330 {
  331     utUnit  unit1, unit2;
  332     char*   string;
  333     double  slope, intercept;
  334 
  335     utIni(&unit1);
  336     utIni(&unit2);
  337     CU_ASSERT_EQUAL(utScan("seconds since 1994-12-15 12:30:00 10", &unit1), 0);
  338     CU_ASSERT_EQUAL(utPrint(&unit1, &string), 0);
  339     CU_ASSERT_EQUAL(utScan(string, &unit2), 0);
  340     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  341     CU_ASSERT_EQUAL(slope, 1);
  342     CU_ASSERT_EQUAL(intercept, 0);
  343 
  344     CU_ASSERT_EQUAL(utScan("seconds since 1994-12-15 2:29:60.0000 UTC", &unit1),
  345     0);
  346     CU_ASSERT_EQUAL(utPrint(&unit1, &string), 0);
  347     CU_ASSERT_EQUAL(utScan(string, &unit2), 0);
  348     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  349     CU_ASSERT_EQUAL(slope, 1);
  350     CU_ASSERT_EQUAL(intercept, 0);
  351 
  352     CU_ASSERT_EQUAL(utScan("seconds since 1925-01-01 00:00:00", &unit1), 0);
  353     CU_ASSERT_EQUAL(utPrint(&unit1, &string), 0);
  354     CU_ASSERT_EQUAL(utScan(string, &unit2), 0);
  355     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  356     CU_ASSERT_EQUAL(slope, 1);
  357     CU_ASSERT_EQUAL(intercept, 0);
  358 
  359     CU_ASSERT_EQUAL(utScan("hours since 1-1-1 00:00:00", &unit1), 0);
  360     CU_ASSERT_EQUAL(utPrint(&unit1, &string), 0);
  361     CU_ASSERT_EQUAL(utScan(string, &unit2), 0);
  362     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  363     CU_ASSERT_EQUAL(slope, 1);
  364     CU_ASSERT_EQUAL(intercept, 0);
  365 
  366     CU_ASSERT_EQUAL(utScan("3600 s since 1-1-1 00:00:00", &unit1), 0);
  367     CU_ASSERT_EQUAL(utPrint(&unit1, &string), 0);
  368     CU_ASSERT_EQUAL(utScan(string, &unit2), 0);
  369     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  370     CU_ASSERT_EQUAL(slope, 1);
  371     CU_ASSERT_EQUAL(intercept, 0);
  372 }
  373 
  374 
  375 static void
  376 test_utAdd(void)
  377 {
  378     utUnit  kilogram;
  379     utUnit  watt;
  380     utUnit  unit;
  381     utUnit  actual;
  382     double  slope, intercept;
  383 
  384     utIni(&kilogram);
  385     utIni(&watt);
  386     utIni(&unit);
  387     utIni(&actual);
  388 
  389     CU_ASSERT_EQUAL_FATAL(utScan("kg", &kilogram), 0);
  390     CU_ASSERT_EQUAL_FATAL(utScan("watt", &watt), 0);
  391     CU_ASSERT_PTR_NOT_NULL_FATAL(utMultiply(&kilogram, &watt, &unit));
  392     CU_ASSERT_EQUAL(utAdd("bof", 1, &unit), 0);
  393     CU_ASSERT_EQUAL_FATAL(utScan("bof", &actual), 0);
  394     CU_ASSERT_EQUAL(utConvert(&unit, &actual, &slope, &intercept), 0);
  395     CU_ASSERT_EQUAL(slope, 1);
  396     CU_ASSERT_EQUAL(intercept, 0);
  397     CU_ASSERT_EQUAL_FATAL(utScan("bofs", &actual), 0);
  398     CU_ASSERT_EQUAL(utConvert(&unit, &actual, &slope, &intercept), 0);
  399     CU_ASSERT_EQUAL(slope, 1);
  400     CU_ASSERT_EQUAL(intercept, 0);
  401 }
  402 
  403 
  404 static void
  405 test_utFind(void)
  406 {
  407     utUnit  unit1, unit2;
  408     double  slope, intercept;
  409 
  410     utIni(&unit1);
  411     utIni(&unit2);
  412 
  413     CU_ASSERT_EQUAL(utFind("watt", &unit1), 0);
  414     CU_ASSERT_EQUAL_FATAL(utScan("watt", &unit2), 0);
  415     CU_ASSERT_EQUAL(utConvert(&unit1, &unit2, &slope, &intercept), 0);
  416     CU_ASSERT_EQUAL(slope, 1);
  417     CU_ASSERT_EQUAL(intercept, 0);
  418 }
  419 
  420 
  421 int
  422 main(
  423     const int       argc,
  424     const char* const*  argv)
  425 {
  426     int exitCode = EXIT_FAILURE;
  427 
  428     if (CU_initialize_registry() == CUE_SUCCESS) {
  429     CU_Suite*   testSuite = CU_add_suite(__FILE__, setup, teardown);
  430 
  431     if (testSuite != NULL) {
  432         CU_ADD_TEST(testSuite, test_utIsInit);
  433         CU_ADD_TEST(testSuite, test_utScan);
  434         CU_ADD_TEST(testSuite, test_utCalendar);
  435         CU_ADD_TEST(testSuite, test_utInvCalendar);
  436         CU_ADD_TEST(testSuite, test_utIsTime);
  437         CU_ADD_TEST(testSuite, test_utHasOrigin);
  438         CU_ADD_TEST(testSuite, test_utClear);
  439         CU_ADD_TEST(testSuite, test_utCopy);
  440         CU_ADD_TEST(testSuite, test_utMultiply);
  441         CU_ADD_TEST(testSuite, test_utDivide);
  442         CU_ADD_TEST(testSuite, test_utInvert);
  443         CU_ADD_TEST(testSuite, test_utRaise);
  444         CU_ADD_TEST(testSuite, test_utShift);
  445         CU_ADD_TEST(testSuite, test_utScale);
  446         CU_ADD_TEST(testSuite, test_utPrint);
  447         CU_ADD_TEST(testSuite, test_utAdd);
  448         CU_ADD_TEST(testSuite, test_utFind);
  449 
  450         ut_set_error_message_handler(ut_ignore);
  451 
  452         if (CU_basic_run_tests() == CUE_SUCCESS) {
  453         if (CU_get_number_of_tests_failed() == 0)
  454             exitCode = EXIT_SUCCESS;
  455         }
  456     }
  457 
  458     CU_cleanup_registry();
  459     }
  460 
  461     return exitCode;
  462 }