LCOV - code coverage report
Current view: top level - test - suite_csvtranscode.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 161 161 100.0 %
Date: 2024-12-05 21:00:54 Functions: 19 19 100.0 %

          Line data    Source code
       1             : /**   @file  suite_csvtranscode.c
       2             :  *   @brief  Check testsuite template
       3             :  *  @author  François Cerbelle (Fanfan), francois@cerbelle.net
       4             :  *
       5             :  *  @internal
       6             :  *       Created:  20/03/2022
       7             :  *      Revision:  none
       8             :  * Last modified:  2024-11-11 21:33
       9             :  *      Compiler:  gcc
      10             :  *  Organization:  Cerbelle.net
      11             :  *     Copyright:  Copyright (c) 2024, François Cerbelle
      12             :  *
      13             :  *  This source code is released for free distribution under the terms of the
      14             :  *  GNU General Public License as published by the Free Software Foundation.
      15             :  */
      16             : 
      17             : #ifdef HAVE_CONFIG_H
      18             : #include "config.h"
      19             : #endif
      20             : 
      21             : #include <check.h>
      22             : #include "checktools.inc"
      23             : 
      24             : #include "csv.c"
      25             : 
      26             : /** Dummy test to trigger fixtures
      27             :  *
      28             :  * This test is defined and added to all testcases in order to execute at
      29             :  * least one test test in each testcase of the testsuite, trigger the
      30             :  * fixtures and have a decent coverage report.
      31             :  */
      32           3 : START_TEST(csvtranscode_test) {
      33             :     /* This test is supposed to trigger a SIGABRT(6) and will crash the
      34             :      * whole testsuite if not caught or not in a child process */
      35           3 :     forktest_only;
      36             : 
      37           2 :     abort();
      38             : }
      39             : END_TEST
      40             : 
      41          69 : int csvtranscode_txt2csv_test(const char* txt, const char* exp) {
      42             :     char* src;
      43             :     char* dst;
      44             :     int retval;
      45          69 :     src=strdup(txt);
      46          69 :     dst = txt2csv(src);
      47          69 :     retval=strcmp(dst,exp);
      48          69 :     free(src);
      49          69 :     free(dst);
      50          69 :     return retval;
      51             : }
      52             : 
      53           3 : START_TEST(csvtranscode_txt2csv) {
      54             : 
      55           3 :     ck_assert(0==csvtranscode_txt2csv_test("",""));
      56           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab","ab"));
      57             : 
      58           3 :     ck_assert(0==csvtranscode_txt2csv_test(",ab","\",ab\""));
      59           3 :     ck_assert(0==csvtranscode_txt2csv_test("a,b","\"a,b\""));
      60           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab,","\"ab,\""));
      61             : 
      62           3 :     ck_assert(0==csvtranscode_txt2csv_test(" ab"," ab"));
      63           3 :     ck_assert(0==csvtranscode_txt2csv_test("a b","a b"));
      64           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab ","ab "));
      65             : 
      66           3 :     ck_assert(0==csvtranscode_txt2csv_test("\rab","\"\rab\""));
      67           3 :     ck_assert(0==csvtranscode_txt2csv_test("a\rb","\"a\rb\""));
      68           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab\r","\"ab\r\""));
      69             : 
      70           3 :     ck_assert(0==csvtranscode_txt2csv_test("\nab","\"\nab\""));
      71           3 :     ck_assert(0==csvtranscode_txt2csv_test("a\nb","\"a\nb\""));
      72           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab\n","\"ab\n\""));
      73             : 
      74           3 :     ck_assert(0==csvtranscode_txt2csv_test("\r\nab","\"\r\nab\""));
      75           3 :     ck_assert(0==csvtranscode_txt2csv_test("a\r\nb","\"a\r\nb\""));
      76           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab\r\n","\"ab\r\n\""));
      77             : 
      78           3 :     ck_assert(0==csvtranscode_txt2csv_test("\n\rab","\"\n\rab\""));
      79           3 :     ck_assert(0==csvtranscode_txt2csv_test("a\n\rb","\"a\n\rb\""));
      80           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab\n\r","\"ab\n\r\""));
      81             : 
      82           3 :     ck_assert(0==csvtranscode_txt2csv_test("\"ab","\"\"\"ab\""));
      83           3 :     ck_assert(0==csvtranscode_txt2csv_test("a\"b","\"a\"\"b\""));
      84           3 :     ck_assert(0==csvtranscode_txt2csv_test("ab\"","\"ab\"\"\""));
      85           3 : }
      86             : END_TEST
      87             : 
      88         144 : int csvtranscode_csvtok_test(char* csv, const char* expected) {
      89         144 :     char* txt = csvtok(csv);
      90         144 :     ck_assert(((NULL!=expected)&&(NULL!=txt))||
      91             :               ((NULL==expected)&&(NULL==txt))) ;
      92         144 :     int retval = (NULL==txt?0:strcmp(txt,expected));
      93         144 :     free(txt);
      94         144 :     return retval;
      95             : }
      96             : 
      97           3 : START_TEST(csvtranscode_csvtok_ok) {
      98           3 :     char* csv = strdup(
      99             :                     " ,a, b,c ,\"\"\"\",\" ,a, b\",c , ,a, b,\"c ,\",\",a, b,c ,\",\"a\rb\",\"a\nb\",\"a\r\nb\",\"a\n\rb\", \r\n"
     100             :                     " ,a, b,c ,\"\"\"\",\" ,a, b\",c , ,a, b,\"c ,\",\",a, b,c ,\",\"a\rb\",\"a\nb\",\"a\r\nb\",\"a\n\rb\", ");
     101             : 
     102           3 :     ck_assert(0==csvtranscode_csvtok_test(csv," "));
     103           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
     104           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
     105           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
     106           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"\""));
     107           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," ,a, b"));
     108           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
     109           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," "));
     110           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
     111           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
     112           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"c ,"));
     113           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,",a, b,c ,"));
     114           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\rb"));
     115           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\nb"));
     116           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\r\nb"));
     117           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\n\rb"));
     118           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," "));
     119             : 
     120           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," "));
     121           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
     122           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
     123           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
     124           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"\""));
     125           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," ,a, b"));
     126           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
     127           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," "));
     128           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
     129           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
     130           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"c ,"));
     131           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,",a, b,c ,"));
     132           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\rb"));
     133           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\nb"));
     134           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\r\nb"));
     135           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"a\n\rb"));
     136           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL," "));
     137             : 
     138           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
     139             : 
     140           3 :     free(csv);
     141           3 : }
     142             : END_TEST
     143             : 
     144           3 : START_TEST(csvtranscode_csvtok_empty) {
     145           3 :     char* csv = strdup("");
     146           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
     147           3 :     free(csv);
     148           3 : }
     149             : END_TEST
     150             : 
     151           3 : START_TEST(csvtranscode_csvtok_commazero) {
     152           3 :     char* csv = strdup("a,");
     153           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
     154           3 :     free(csv);
     155           3 : }
     156             : END_TEST
     157             : 
     158           3 : START_TEST(csvtranscode_csvtok_commaCR) {
     159           3 :     char* csv = strdup("a,\r");
     160             :     /* Accepted because CR is not a separator by itself, alone, but a contents */
     161           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
     162           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,"\r"));
     163           3 :     free(csv);
     164           3 : }
     165             : END_TEST
     166             : 
     167           3 : START_TEST(csvtranscode_csvtok_commaLF) {
     168           3 :     char* csv = strdup("a,\n");
     169           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
     170           3 :     free(csv);
     171           3 : }
     172             : END_TEST
     173             : 
     174           3 : START_TEST(csvtranscode_csvtok_commaCRLF) {
     175           3 :     char* csv = strdup("a,\r\n");
     176           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
     177           3 :     free(csv);
     178           3 : }
     179             : END_TEST
     180             : 
     181           3 : START_TEST(csvtranscode_csvtok_commaLFCR) {
     182           3 :     char* csv = strdup("a,\n\r");
     183           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
     184           3 :     free(csv);
     185           3 : }
     186             : END_TEST
     187             : 
     188           3 : START_TEST(csvtranscode_csvtok_spacebeforequote) {
     189           3 :     char* csv = strdup("a, \"b\",c");
     190           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
     191           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
     192           3 :     free(csv);
     193           3 : }
     194             : END_TEST
     195             : 
     196           3 : START_TEST(csvtranscode_csvtok_spaceafterquote) {
     197           3 :     char* csv = strdup("a,\"b\" ,c");
     198           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
     199           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
     200           3 :     free(csv);
     201           3 : }
     202             : END_TEST
     203             : 
     204           3 : START_TEST(csvtranscode_csvtok_unbalancedquotes) {
     205           3 :     char* csv = strdup("a,\"b,c");
     206           3 :     ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
     207           3 :     ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
     208           3 :     free(csv);
     209           3 : }
     210             : END_TEST
     211             : 
     212             : /************************************************************************/
     213             : /* Begining of test (potentially in child) */
     214          36 : void csvtranscode_checked_uninitialized_setup() {
     215          36 :     signals_catch();
     216          36 :     forktest_gprofdir();
     217          36 : }
     218             : 
     219             : /* End of test (potentially in child) */
     220          34 : void csvtranscode_checked_uninitialized_teardown() {
     221          34 :     signals_release();
     222          34 : }
     223             : /************************************************************************/
     224          27 : void csvtranscode_unchecked_common_setup() {
     225          27 :     forktest_init();
     226          27 : }
     227           3 : void csvtranscode_unchecked_common_teardown() {
     228           3 : }
     229             : /************************************************************************/
     230          28 : Suite* csvtranscode_suite() {
     231             :     Suite *s;
     232             :     TCase *tc;
     233             : 
     234          28 :     s = suite_create("CSVTranscode");
     235             : 
     236          28 :     tc = tcase_create("csvtok");
     237          28 :     tcase_set_tags(tc,"CSVTag CSVTag2");
     238             :     /* tcase_set_timeout(tc,5); */ /* seconds */
     239          28 :     tcase_add_checked_fixture(tc, csvtranscode_checked_uninitialized_setup, csvtranscode_checked_uninitialized_teardown);
     240          28 :     tcase_add_unchecked_fixture(tc, csvtranscode_unchecked_common_setup, csvtranscode_unchecked_common_teardown);
     241          28 :     suite_add_tcase(s, tc);
     242          28 :     tcase_add_test_raise_signal(tc, csvtranscode_test,6);
     243          28 :     tcase_add_test(tc, csvtranscode_txt2csv);
     244          28 :     tcase_add_test(tc, csvtranscode_csvtok_empty);
     245          28 :     tcase_add_test(tc, csvtranscode_csvtok_ok);
     246          28 :     tcase_add_test(tc, csvtranscode_csvtok_commazero);
     247          28 :     tcase_add_test(tc, csvtranscode_csvtok_commaLF);
     248          28 :     tcase_add_test(tc, csvtranscode_csvtok_commaCR);
     249          28 :     tcase_add_test(tc, csvtranscode_csvtok_commaCRLF);
     250          28 :     tcase_add_test(tc, csvtranscode_csvtok_commaLFCR);
     251          28 :     tcase_add_test(tc, csvtranscode_csvtok_spacebeforequote);
     252          28 :     tcase_add_test(tc, csvtranscode_csvtok_spaceafterquote);
     253          28 :     tcase_add_test(tc, csvtranscode_csvtok_unbalancedquotes);
     254             : 
     255          28 :     return s;
     256             : }
     257             : /* vim: set tw=80: */

Generated by: LCOV version 1.16