2018-08-23 18:53:43 +03:00
/*
* Copyright ( C ) 2018 Red Hat , Inc .
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library . If not , see
* < http : //www.gnu.org/licenses/>.
*/
# include <config.h>
# include <locale.h>
2020-09-01 14:27:44 +03:00
# ifdef WITH_XLOCALE_H
2020-01-27 13:59:19 +03:00
# include <xlocale.h>
# endif
2020-01-17 16:40:54 +03:00
# include <wchar.h>
2018-09-04 13:26:03 +03:00
# include <wctype.h>
2018-08-23 18:53:43 +03:00
# include "internal.h"
# include "testutils.h"
# include "../tools/vsh-table.h"
static int
2019-10-14 15:45:03 +03:00
testVshTableNew ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-12-14 17:26:47 +03:00
g_autoptr ( vshTable ) table = vshTableNew ( NULL , NULL ) ;
2021-12-15 12:23:15 +03:00
if ( table ) {
2018-08-23 18:53:43 +03:00
fprintf ( stderr , " expected failure when passing null to vshTableNew \n " ) ;
return - 1 ;
}
return 0 ;
}
static int
2019-10-14 15:45:03 +03:00
testVshTableHeader ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
int ret = 0 ;
2021-11-08 14:49:16 +03:00
g_autofree char * act = NULL ;
g_autofree char * act2 = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp =
2019-02-12 12:00:38 +03:00
" 1 fedora28 running \n "
" 2 rhel7.5 running \n " ;
2018-08-23 18:53:43 +03:00
const char * exp2 =
2019-02-12 12:00:38 +03:00
" Id Name State \n "
" -------------------------- \n "
" 1 fedora28 running \n "
" 2 rhel7.5 running \n " ;
2018-08-23 18:53:43 +03:00
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = vshTableNew ( " Id " , " Name " , " State " ,
2018-08-23 18:53:43 +03:00
NULL ) ; //to ask about return
if ( ! table )
2021-11-08 14:49:16 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table , " 1 " , " fedora28 " , " running " , NULL ) ;
vshTableRowAppend ( table , " 2 " , " rhel7.5 " , " running " ,
NULL ) ;
act = vshTablePrintToString ( table , false ) ;
if ( virTestCompareToString ( exp , act ) < 0 )
ret = - 1 ;
2021-11-08 14:49:16 +03:00
act2 = vshTablePrintToString ( table , true ) ;
if ( virTestCompareToString ( exp2 , act2 ) < 0 )
2018-08-23 18:53:43 +03:00
ret = - 1 ;
return ret ;
}
static int
2019-10-14 15:45:03 +03:00
testVshTableRowAppend ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = vshTableNew ( " Id " , " Name " , NULL ) ;
2018-08-23 18:53:43 +03:00
if ( ! table )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-12-14 17:26:47 +03:00
if ( vshTableRowAppend ( table , NULL , NULL ) > = 0 ) {
2018-08-23 18:53:43 +03:00
fprintf ( stderr , " Appending NULL shouldn't work \n " ) ;
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
}
if ( vshTableRowAppend ( table , " 2 " , NULL ) > = 0 ) {
fprintf ( stderr , " Appending less items than in header \n " ) ;
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
}
if ( vshTableRowAppend ( table , " 2 " , " rhel7.5 " , " running " ,
NULL ) > = 0 ) {
fprintf ( stderr , " Appending more items than in header \n " ) ;
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
}
if ( vshTableRowAppend ( table , " 2 " , " rhel7.5 " , NULL ) < 0 ) {
fprintf ( stderr , " Appending same number of items as in header "
" should not return NULL \n " ) ;
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
}
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
static int
2019-10-14 15:45:03 +03:00
testUnicode ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 23:37:44 +03:00
g_autofree char * act = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp =
2019-02-12 12:00:38 +03:00
" Id 名稱 государство \n "
" ----------------------------------------- \n "
" 1 fedora28 running \n "
" 2 つへソrhel7.5つへソ running \n " ;
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = NULL ;
2018-08-23 18:53:43 +03:00
table = vshTableNew ( " Id " , " 名稱 " , " государство " , NULL ) ;
if ( ! table )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table , " 1 " , " fedora28 " , " running " , NULL ) ;
2018-08-29 10:48:10 +03:00
vshTableRowAppend ( table , " 2 " , " つへソrhel7.5つへソ " , " running " ,
2018-08-23 18:53:43 +03:00
NULL ) ;
act = vshTablePrintToString ( table , true ) ;
if ( virTestCompareToString ( exp , act ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
2020-01-24 23:30:04 +03:00
/* Point of this test is to see how table behaves with right to left writing */
2018-08-23 18:53:43 +03:00
static int
2019-10-14 15:45:03 +03:00
testUnicodeArabic ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 23:37:44 +03:00
g_autofree char * act = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp =
2019-02-12 12:00:38 +03:00
" ﻡﺍ ﻢﻣﺍ ﻕﺎﺌﻣﺓ ﺓ ﺎﻠﺼﻋ ﺍﻸﺜﻧﺎﻧ \n "
" ------------------------------------------------------------------------------------------- \n "
" 1 ﻉﺪﻴﻟ ﺎﻠﺜﻘﻴﻟ ﻕﺎﻣ ﻊﻧ, ٣٠ ﻎﻴﻨﻳﺍ ﻮﺘﻧﺎﻤﺗ ﺎﻠﺛﺎﻠﺛ، ﺄﺳﺭ, ﺩﻮﻟ ﺩﻮﻟ. ﺄﻣﺎﻣ ﺍ ﺎﻧ ﻲﻜﻧ \n "
" ﺺﻔﺣﺓ ﺖﻜﺘﻴﻛﺍً ﻊﻟ, ﺎﻠﺠﻧﻭﺩ ﻭﺎﻠﻌﺗﺍﺩ ﺵﺭ \n " ;
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = NULL ;
2018-09-04 13:26:03 +03:00
wchar_t wc ;
/* If this char is not classed as printable, the actual
* output won ' t match what this test expects . The code
* is still operating correctly , but we have different
* layout */
mbrtowc ( & wc , " ، " , MB_CUR_MAX , NULL ) ;
if ( ! iswprint ( wc ) )
return EXIT_AM_SKIP ;
2018-08-23 18:53:43 +03:00
table = vshTableNew ( " ﻡﺍ ﻢﻣﺍ ﻕﺎﺌﻣﺓ " , " ﺓ ﺎﻠﺼﻋ " , " ﺍﻸﺜﻧﺎﻧ " , NULL ) ;
if ( ! table )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table ,
" 1 " ,
" ﻉﺪﻴﻟ ﺎﻠﺜﻘﻴﻟ ﻕﺎﻣ ﻊﻧ, ٣٠ ﻎﻴﻨﻳﺍ ﻮﺘﻧﺎﻤﺗ ﺎﻠﺛﺎﻠﺛ، ﺄﺳﺭ, ﺩﻮﻟ " ,
" ﺩﻮﻟ. ﺄﻣﺎﻣ ﺍ ﺎﻧ ﻲﻜﻧ " ,
NULL ) ;
vshTableRowAppend ( table , " ﺺﻔﺣﺓ " , " ﺖﻜﺘﻴﻛﺍً ﻊﻟ, ﺎﻠﺠﻧﻭﺩ ﻭﺎﻠﻌﺗﺍﺩ " , " ﺵﺭ " ,
NULL ) ;
act = vshTablePrintToString ( table , true ) ;
if ( virTestCompareToString ( exp , act ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
/* Testing zero-width characters by inserting few zero-width spaces */
static int
2019-10-14 15:45:03 +03:00
testUnicodeZeroWidthChar ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp =
2019-02-12 12:00:38 +03:00
" I \u200B d Name \u200B Status \n "
" -------------------------- \n "
" 1 \u200B fedora28 run \u200B ning \n "
" 2 rhel7.5 running \n " ;
2021-09-04 23:37:44 +03:00
g_autofree char * act = NULL ;
2018-09-04 13:26:03 +03:00
wchar_t wc ;
/* If this char is not classed as printable, the actual
* output won ' t match what this test expects . The code
* is still operating correctly , but we have different
* layout */
mbrtowc ( & wc , " \u200B " , MB_CUR_MAX , NULL ) ;
if ( ! iswprint ( wc ) )
return EXIT_AM_SKIP ;
2018-08-23 18:53:43 +03:00
table = vshTableNew ( " I \u200B d " , " Name " , " \u200B Status " , NULL ) ;
if ( ! table )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table , " 1 \u200B " , " fedora28 " , " run \u200B ning " , NULL ) ;
vshTableRowAppend ( table , " 2 " , " rhel7.5 " , " running " , NULL ) ;
act = vshTablePrintToString ( table , true ) ;
if ( virTestCompareToString ( exp , act ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
static int
2019-10-14 15:45:03 +03:00
testUnicodeCombiningChar ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp =
2019-02-12 12:00:38 +03:00
" Id Náme Ⓢtatus \n "
" -------------------------- \n "
" 1 fědora28 running \n "
" 2 rhel running \n " ;
2021-09-04 23:37:44 +03:00
g_autofree char * act = NULL ;
2018-08-23 18:53:43 +03:00
table = vshTableNew ( " Id " , " Náme " , " Ⓢtatus " , NULL ) ;
if ( ! table )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table , " 1 " , " fědora28 " , " running " , NULL ) ;
vshTableRowAppend ( table , " 2 " , " rhel " , " running " , NULL ) ;
act = vshTablePrintToString ( table , true ) ;
if ( virTestCompareToString ( exp , act ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
/* Testing zero-width characters by inserting few zero-width spaces */
static int
2019-10-14 15:45:03 +03:00
testUnicodeNonPrintableChar ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp =
2019-02-12 12:00:38 +03:00
" I \\ x09d Name Status \n "
" ---------------------------------- \n "
" 1 f \\ x07edora28 running \n "
" 2 rhel7.5 running \n " ;
2021-09-04 23:37:44 +03:00
g_autofree char * act = NULL ;
2018-08-23 18:53:43 +03:00
table = vshTableNew ( " I \t d " , " Name " , " Status " , NULL ) ;
if ( ! table )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table , " 1 " , " f \a edora28 " , " running " , NULL ) ;
vshTableRowAppend ( table , " 2 " , " rhel7.5 " , " running " , NULL ) ;
act = vshTablePrintToString ( table , true ) ;
if ( virTestCompareToString ( exp , act ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
static int
2019-10-14 15:45:03 +03:00
testNTables ( const void * opaque G_GNUC_UNUSED )
2018-08-23 18:53:43 +03:00
{
2021-09-04 21:57:24 +03:00
g_autoptr ( vshTable ) table1 = NULL ;
g_autoptr ( vshTable ) table2 = NULL ;
g_autoptr ( vshTable ) table3 = NULL ;
2018-08-23 18:53:43 +03:00
const char * exp1 =
2019-02-12 12:00:38 +03:00
" Id Name Status \n "
" -------------------------- \n "
" 1 fedora28 running \n "
2019-02-12 12:02:38 +03:00
" 2 rhel7.5 running \n "
" 3 gazpacho \n " ;
2018-08-23 18:53:43 +03:00
const char * exp2 =
2019-02-12 12:00:38 +03:00
" Id Name Status \n "
" --------------------- \n " ;
2018-08-23 18:53:43 +03:00
const char * exp3 =
2019-02-12 12:00:38 +03:00
" Id \n "
" ----- \n "
" 1 \n "
" 2 \n "
" 3 \n "
" 4 \n " ;
2021-09-04 23:37:44 +03:00
g_autofree char * act1 = NULL ;
g_autofree char * act2 = NULL ;
g_autofree char * act3 = NULL ;
2018-08-23 18:53:43 +03:00
table1 = vshTableNew ( " Id " , " Name " , " Status " , NULL ) ;
if ( ! table1 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table1 , " 1 " , " fedora28 " , " running " , NULL ) ;
vshTableRowAppend ( table1 , " 2 " , " rhel7.5 " , " running " , NULL ) ;
2019-02-12 12:02:38 +03:00
vshTableRowAppend ( table1 , " 3 " , " gazpacho " , " " , NULL ) ;
2018-08-23 18:53:43 +03:00
act1 = vshTablePrintToString ( table1 , true ) ;
table2 = vshTableNew ( " Id " , " Name " , " Status " , NULL ) ;
if ( ! table2 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
act2 = vshTablePrintToString ( table2 , true ) ;
table3 = vshTableNew ( " Id " , NULL ) ;
if ( ! table3 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
vshTableRowAppend ( table3 , " 1 " , NULL ) ;
vshTableRowAppend ( table3 , " 2 " , NULL ) ;
vshTableRowAppend ( table3 , " 3 " , NULL ) ;
vshTableRowAppend ( table3 , " 4 " , NULL ) ;
act3 = vshTablePrintToString ( table3 , true ) ;
if ( virTestCompareToString ( exp1 , act1 ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
if ( virTestCompareToString ( exp2 , act2 ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
if ( virTestCompareToString ( exp3 , act3 ) < 0 )
2021-09-04 23:40:50 +03:00
return - 1 ;
2018-08-23 18:53:43 +03:00
2021-09-04 23:40:50 +03:00
return 0 ;
2018-08-23 18:53:43 +03:00
}
static int
mymain ( void )
{
int ret = 0 ;
if ( ! setlocale ( LC_CTYPE , " en_US.UTF-8 " ) )
return EXIT_AM_SKIP ;
if ( virTestRun ( " testVshTableNew " , testVshTableNew , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testVshTableHeader " , testVshTableHeader , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testVshTableRowAppend " , testVshTableRowAppend , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testUnicode " , testUnicode , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testUnicodeArabic " , testUnicodeArabic , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testUnicodeZeroWidthChar " ,
testUnicodeZeroWidthChar ,
NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testUnicodeCombiningChar " ,
testUnicodeCombiningChar ,
NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testUnicodeNonPrintableChar " ,
testUnicodeNonPrintableChar ,
NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " testNTables " , testNTables , NULL ) < 0 )
ret = - 1 ;
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
VIR_TEST_MAIN ( mymain )