2013-02-11 00:56:03 +04:00
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd .
Copyright 2013 Thomas H . P . Andersen
systemd 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 .
systemd 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 systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
# include <string.h>
# include "util.h"
# include "strv.h"
# include "strxcpyx.h"
static void test_strpcpy ( void ) {
char target [ 25 ] ;
char * s = target ;
size_t space_left ;
space_left = sizeof ( target ) ;
space_left = strpcpy ( & s , space_left , " 12345 " ) ;
space_left = strpcpy ( & s , space_left , " hey hey hey " ) ;
space_left = strpcpy ( & s , space_left , " waldo " ) ;
space_left = strpcpy ( & s , space_left , " ba " ) ;
space_left = strpcpy ( & s , space_left , " r " ) ;
space_left = strpcpy ( & s , space_left , " foo " ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " 12345hey hey heywaldobar " ) ) ;
assert_se ( space_left = = 0 ) ;
2013-02-11 00:56:03 +04:00
}
static void test_strpcpyf ( void ) {
char target [ 25 ] ;
char * s = target ;
size_t space_left ;
space_left = sizeof ( target ) ;
2013-03-27 07:47:25 +04:00
space_left = strpcpyf ( & s , space_left , " space left: %zd. " , space_left ) ;
2013-02-11 00:56:03 +04:00
space_left = strpcpyf ( & s , space_left , " foo%s " , " bar " ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " space left: 25. foobar " ) ) ;
assert_se ( space_left = = 3 ) ;
2013-02-11 00:56:03 +04:00
}
static void test_strpcpyl ( void ) {
char target [ 25 ] ;
char * s = target ;
size_t space_left ;
space_left = sizeof ( target ) ;
space_left = strpcpyl ( & s , space_left , " waldo " , " test " , " waldo. " , NULL ) ;
space_left = strpcpyl ( & s , space_left , " Banana " , NULL ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " waldo test waldo. Banana " ) ) ;
assert_se ( space_left = = 1 ) ;
2013-02-11 00:56:03 +04:00
}
static void test_strscpy ( void ) {
char target [ 25 ] ;
size_t space_left ;
space_left = sizeof ( target ) ;
space_left = strscpy ( target , space_left , " 12345 " ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " 12345 " ) ) ;
assert_se ( space_left = = 20 ) ;
2013-02-11 00:56:03 +04:00
}
static void test_strscpyl ( void ) {
char target [ 25 ] ;
size_t space_left ;
space_left = sizeof ( target ) ;
space_left = strscpyl ( target , space_left , " 12345 " , " waldo " , " waldo " , NULL ) ;
2014-10-05 01:51:45 +04:00
assert_se ( streq ( target , " 12345waldowaldo " ) ) ;
assert_se ( space_left = = 10 ) ;
2013-02-11 00:56:03 +04:00
}
int main ( int argc , char * argv [ ] ) {
test_strpcpy ( ) ;
test_strpcpyf ( ) ;
test_strpcpyl ( ) ;
test_strscpy ( ) ;
test_strscpyl ( ) ;
return 0 ;
}