📄 test_yc_dyrscarr.c
字号:
#ifdef _MSC_VER
#pragma warning(disable:4127)
#pragma warning(disable:4244)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../young/youngc.h"
#ifdef _MSC_VER
#pragma warning(disable:4127)
#endif
#ifdef __cplusplus
using namespace youngc;
#endif
void DestroyString( void* pdstr )
{
if( ncstr_size( (ncstring*)pdstr ) > 0 )
printf( "~~~~destroy string: %s\n", ncstr_to_string((ncstring*)pdstr) );
ncstr_destroy( (ncstring*)pdstr );
}
void PrintStrArray( dyrscarray* pstrarr )
{
size_t i = 0;
size_t len = dyrscarr_size( pstrarr );
ncstring* pstr = (ncstring*)dyrscarr_begin( pstrarr );
for( ; i < len; ++i, ++pstr )
{
printf( "string[%d]: %s\n", i, ncstr_to_string(pstr) );
}
printf( "\n" );
}
void GetStrings( dyrscarray* pstrarr, ncstring* pdstr )
{
while( 1 )
{
printf( "\nplease input strings, input '0' to exit!\n" );
ncstr_getline( pdstr, '\n' );
if( ncstr_size(pdstr) == 0 )
continue;
if( strcmp("0", ncstr_to_string(pdstr)) == 0 )
break;
/* MACRO_DYRSCARR_PUSH_BACK_FUN( *pstrarr, *pdstr, ncstring );
*/ dyrscarr_push_back( pstrarr, pdstr );
}
}
void test_dyrscarr(void)
{
char get[8] = {0};
ncstring dstr;
dyrscarray strarr;
ncstr_init( &dstr );
printf( "please set move function(y/n)\n" );
scanf( "%s", get );
if( get[0] == 'y' )
dyrscarr_init( &strarr, sizeof(ncstring), (ylib_fp_oper_t)ncstr_init,
(ylib_fp_copy_t)ncstr_init_copy, (ylib_fp_copy_t)ncstr_assign_copy,
(ylib_fp_move_t)ncstr_init_move, (ylib_fp_move_t)ncstr_assign_move,
DestroyString, pool_alloc, pool_free );
else
dyrscarr_init( &strarr, sizeof(ncstring), (ylib_fp_oper_t)ncstr_init,
(ylib_fp_copy_t)ncstr_init_copy, (ylib_fp_copy_t)ncstr_assign_copy,
NULL, NULL, DestroyString, pool_alloc, pool_free );
while( 1 )
{
int choice = 0;
printf( "\n\n" );
printf( " 1: test dyrscarr_init_copy\n" );
printf( " 2: test dyrscarr_assign_copy\n" );
printf( " 3: test dyrscarr_init_move\n" );
printf( " 4: test dyrscarr_assign_move\n" );
printf( " 5: test dyrscarr_reserve\n" );
printf( " 6: test dyrscarr_erase_pos\n" );
printf( " 7: test dyrscarr_erase_range\n" );
printf( " 8: test dyrscarr_insert_value\n" );
printf( " 9: test dyrscarr_insert_array\n" );
printf( " 10: test dyrscarr_push_back\n" );
printf( " 11: test dyrscarr_resize\n" );
printf( " 12: test dyrscarr_replace_fill\n" );
printf( " 13: test dyrscarr_replace_array\n" );
printf( " 14: test dyrscarr_max_size\n" );
printf( " 15: test dyrscarr_size\n" );
printf( " 16: test dyrscarr_capacity\n" );
printf( " 17: test dyrscarr_space\n" );
printf( " 18: test dyrscarr_begin\n" );
printf( " 19: test dyrscarr_end\n" );
printf( " 20: test dyrscarr_front\n" );
printf( " 21: test dyrscarr_back\n" );
printf( " 22: test dyrscarr_index\n" );
printf( " 23: test dyrscarr_at\n" );
printf( " 24: test dyrscarr_pop_back\n" );
printf( " 25: test dyrscarr_reverse\n" );
printf( " 26: test string sort\n" );
printf( " 27: print string array\n" );
printf( " 0: exit\n\n\n" );
scanf( "%d", &choice );
switch( choice )
{
case 0:
goto EXIT;
case 1:
{
dyrscarray strarr1;
dyrscarr_init_copy( &strarr1, &strarr );
printf( "\nsrc:\n" );
PrintStrArray( &strarr );
printf( "dst:\n" );
PrintStrArray( &strarr1 );
dyrscarr_destroy( &strarr );
dyrscarr_init_copy( &strarr, &strarr1 );
dyrscarr_destroy( &strarr1 );
break;
}
case 2:
{
dyrscarray strarr1;
dyrscarr_init( &strarr1, sizeof(ncstring),(ylib_fp_oper_t)ncstr_init,
(ylib_fp_copy_t)ncstr_init_copy, (ylib_fp_copy_t)ncstr_assign_copy,
(ylib_fp_move_t)ncstr_init_move, (ylib_fp_move_t)ncstr_assign_move,
DestroyString, pool_alloc, pool_free );
printf( "\n\nbefore dst:\n" );
PrintStrArray( &strarr1 );
dyrscarr_assign_copy( &strarr1, &strarr );
printf( "\nsrc:\n" );
PrintStrArray( &strarr );
printf( "after dst:\n" );
PrintStrArray( &strarr1 );
dyrscarr_destroy( &strarr );
dyrscarr_assign_copy( &strarr, &strarr1 );
dyrscarr_destroy( &strarr1 );
break;
}
case 3:
{
dyrscarray strarr1;
GetStrings( &strarr, &dstr );
dyrscarr_init_move( &strarr1, &strarr );
printf( "\nsrc:\n" );
PrintStrArray( &strarr );
printf( "dst:\n" );
PrintStrArray( &strarr1 );
dyrscarr_destroy( &strarr1 );
break;
}
case 4:
{
dyrscarray strarr1;
dyrscarr_init( &strarr1, sizeof(ncstring),(ylib_fp_oper_t)ncstr_init,
(ylib_fp_copy_t)ncstr_init_copy, (ylib_fp_copy_t)ncstr_assign_copy,
(ylib_fp_move_t)ncstr_init_move, (ylib_fp_move_t)ncstr_assign_move,
DestroyString, pool_alloc, pool_free );
printf( "please input first string array:\n" );
GetStrings( &strarr, &dstr );
printf( "\n\nplease input second string array:\n" );
GetStrings( &strarr1, &dstr );
printf( "\n\nbefore dst:\n" );
PrintStrArray( &strarr1 );
dyrscarr_assign_move( &strarr1, &strarr );
printf( "\nsrc:\n" );
PrintStrArray( &strarr );
printf( "after dst:\n" );
PrintStrArray( &strarr1 );
dyrscarr_destroy( &strarr1 );
break;
}
case 5:
{
size_t capa = 0;
printf( "before length: %u\n", dyrscarr_size(&strarr) );
printf( "before capacity: %u\n", dyrscarr_capacity(&strarr) );
printf( "please input new capacity:\n" );
scanf( "%u", &capa );
if( false == dyrscarr_reserve(&strarr, capa) )
printf( "return false!\n" );
else
{
printf( "after length: %u\n", dyrscarr_size(&strarr) );
printf( "after capacity: %u\n", dyrscarr_capacity(&strarr) );
}
break;
}
case 6:
{
size_t pos = 0;
printf( "please input index:\n" );
scanf( "%u", &pos );
dyrscarr_erase_pos( &strarr, pos );
break;
}
case 7:
{
size_t first = 0, last = 0;
printf( "please input first index:\n" );
scanf( "%u", &first );
printf( "please input last index:\n" );
scanf( "%u", &last );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -