2017-09-21 22:26:06 +03:00
/*
2013-08-15 17:03:21 +04:00
* Copyright ( C ) 2013 Colin Walters < walters @ verbum . org >
*
2018-01-30 22:26:26 +03:00
* SPDX - License - Identifier : LGPL - 2.0 +
*
2013-08-15 17:03:21 +04:00
* 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 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
2021-12-07 04:20:55 +03:00
* License along with this library . If not , see < https : //www.gnu.org/licenses/>.
2013-08-15 17:03:21 +04:00
*/
# include "config.h"
2015-05-05 01:08:49 +03:00
# include "libglnx.h"
2013-08-15 17:03:21 +04:00
# include "ostree-varint.h"
static void
2023-05-01 21:24:29 +03:00
check_one_roundtrip ( guint64 val )
2013-08-15 17:03:21 +04:00
{
2023-05-01 21:24:29 +03:00
g_autoptr ( GString ) buf = g_string_new ( NULL ) ;
2013-08-15 17:03:21 +04:00
guint64 newval ;
gsize bytes_read ;
_ostree_write_varuint64 ( buf , val ) ;
if ( g_test_verbose ( ) )
{
2023-05-01 21:24:29 +03:00
g_autoptr ( GVariant ) v
= g_variant_new_from_data ( G_VARIANT_TYPE ( " ay " ) , buf - > str , buf - > len , TRUE , NULL , NULL ) ;
2015-05-05 00:58:26 +03:00
g_autofree char * data = g_variant_print ( v , FALSE ) ;
2013-08-15 17:03:21 +04:00
g_test_message ( " % " G_GUINT64_FORMAT " -> %s " , val , data ) ;
}
2023-05-01 21:24:29 +03:00
g_assert ( _ostree_read_varuint64 ( ( guint8 * ) buf - > str , buf - > len , & newval , & bytes_read ) ) ;
2013-08-15 17:03:21 +04:00
g_assert_cmpint ( bytes_read , < = , 10 ) ;
g_assert_cmpint ( val , = = , newval ) ;
}
static void
test_roundtrips ( void )
{
2023-05-01 21:24:29 +03:00
const guint64 test_inputs [ ] = { 0 ,
1 ,
0x6F ,
0xA0 ,
0xFF ,
0xF0F0 ,
0xCAFE ,
0xCAFEBABE ,
G_MAXUINT64 ,
G_MAXUINT64 - 1 ,
G_MAXUINT64 / 2 } ;
2013-08-15 17:03:21 +04:00
guint i ;
for ( i = 0 ; i < G_N_ELEMENTS ( test_inputs ) ; i + + )
check_one_roundtrip ( test_inputs [ i ] ) ;
}
int
main ( int argc , char * * argv )
{
2022-01-10 13:22:28 +03:00
gboolean is_ok = g_setenv ( " GIO_USE_VFS " , " local " , TRUE ) ;
g_assert ( is_ok = = TRUE ) ;
2013-08-15 17:03:21 +04:00
g_test_init ( & argc , & argv , NULL ) ;
g_test_add_func ( " /ostree/varint " , test_roundtrips ) ;
return g_test_run ( ) ;
}