⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_yc.c

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 C
字号:
#ifdef _MSC_VER
    #pragma warning(disable:4127)
    #pragma warning(disable:4244)
#endif

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../young/youngc.h"

#ifdef  __cplusplus
    using namespace youngc;
#endif

extern void test_dstring(void);
extern void test_dyrscarr(void);
extern void test_chkarray(void);
extern void test_dblnklst(void);
extern void test_sglnklst(void);
extern void test_tree(void);
extern void test_hashtable(void);
extern void test_string( void );


int arr[] = { 17, 3,  5,  -4, 1, 12, -10, -1, 14, 7,
              -6, 8, 15, -11, 2, -1,  18,  4, -1, 0,
              -34, -2, 13, -9876, -23, 0, 14, 74, 98, -135 };


void print_int( void* ptr )
{
     printf( "%d, ", *((int*)ptr) );
}

void init_int( void* ptr )
{
    *((int*)ptr) = 0;
}

int copy_int( void* lhs, const void* rhs )
{
    *((int*)lhs) = *((const int*)rhs);
    return 1;
}

void destroy_int( void* ptr )
{
     printf( "~~~~destroy int: %d\n", *((int*)ptr) );
}

void swap_int( void* lhs, void* rhs )
{
    int tmp = *((int*)lhs);
    *((int*)lhs) = *((int*)rhs);
    *((int*)rhs) = tmp;
}

#define  ARRAYSIZE  1024
void test_copytime(void)
{
    const int cycle = 1000000;
    double sec;
    clock_t begin, end;
    int i;
    char dst[ARRAYSIZE];
    char src[ARRAYSIZE];

    begin = clock();
    for( i = 0; i < cycle; ++i )
        ylib_memcopy( dst, src, sizeof(char) * ARRAYSIZE );
    end = clock();
    sec = (double)(end - begin) / (double)CLOCKS_PER_SEC;
    printf( "\nbegin = %ld, end = %ld, ylib_memcopy time = %f\n",
            begin, end, sec );

    begin = clock();
    for( i = 0; i < cycle; ++i )
        memcpy( dst, src, sizeof(char) * ARRAYSIZE );
    end = clock();
    sec = (double)(end - begin) / (double)CLOCKS_PER_SEC;
    printf( "\nbegin = %ld, end = %ld, memcpy time = %f\n",
            begin, end, sec );
}

void test_algo( void )
{
    int tmp, intarr[60];
    const int* result;
    pair_pointer pptr;

    printf( "rforeach = " );
    rforeach( arr + 30, 30, sizeof(int), print_int );  putchar( '\n' );

    memreverse( arr, 30, sizeof(int) );
    printf( "memreverse = " );
    foreach( arr, 30, sizeof(int), print_int );  putchar( '\n' );

    ylib_memcopy( intarr, arr, sizeof(int) * 30 );

    tmp = -10;
    result = (const int*)find( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "find %d: %d\n", tmp, result ? result - arr + 1 : -1 );
    tmp = 100;
    result = (const int*)find( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "find %d: %d\n", tmp, result ? result - arr + 1 : -1 );

    result = (const int*)search( arr, 30, intarr + 13, 3, sizeof(int), cmp_int );
    printf( "search: %d\n", result ? result - arr + 1 : -1 );
    result = (const int*)search( arr, 30, intarr + 13, 13, sizeof(int), cmp_int );
    printf( "search: %d\n", result ? result - arr + 1 : -1 );

    printf( "matching: %s\n",
            matching(arr, 30, intarr + 13, 30, sizeof(int), cmp_int)
            ? "true" : "false" );
    printf( "matching: %s\n",
            matching(arr, 30, intarr, 30, sizeof(int), cmp_int)
            ? "true" : "false" );

    printf( "quicksort = " );
    quicksort( arr, 30, sizeof(int), cmp_int, swap_int );
    foreach( arr, 30, sizeof(int), print_int );  putchar( '\n' );

    tmp = 18;
    result = (const int*)lower_bound( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "lower_bound %d: %d\n", tmp, result ? result - arr + 1 : -1 );
    tmp = 180;
    result = (const int*)lower_bound( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "lower_bound %d: %d\n", tmp, result ? result - arr + 1 : -1 );

    tmp = -10;
    result = (const int*)upper_bound( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "upper_bound %d: %d\n", tmp, result ? result - arr + 1 : -1 );
    tmp = 180;
    result = (const int*)upper_bound( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "upper_bound %d: %d\n", tmp, result ? result - arr + 1 : -1 );

    tmp = -1;
    pptr = equal_range( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "equal_range %d: [%d, %d)\n", tmp,
            pptr.first ? (int*)(pptr.first) - arr + 1 : -1,
            pptr.second ? (int*)(pptr.second) - arr + 1 : -1 );
    tmp = 180;
    pptr = equal_range( arr, 30, sizeof(int), &tmp, cmp_int );
    printf( "equal_range %d: [%d, %d)\n", tmp,
            pptr.first ? (int*)(pptr.first) - arr + 1 : -1,
            pptr.second ? (int*)(pptr.second) - arr + 1 : -1 );

    tmp = -1;
    printf( "count of %d = %u\n", tmp, count(arr, 30, sizeof(int), &tmp, cmp_int) );
    printf( "min_element = %d\n", *((int*)min_element(arr, 30, sizeof(int), cmp_int)) );
    printf( "max_element = %d\n", *((int*)max_element(arr, 30, sizeof(int), cmp_int)) );
}

int main( void )
{
#ifdef __STDC__
    printf( "Standard C Compiler!\n" );
#endif
#ifdef __cplusplus
    printf( "C++ Compiler!\n" );
#endif
#ifdef __STDC_VERSION__
    printf( "Standard C Version: %ld\n", __STDC_VERSION__ );
#endif
#if !defined(__STDC__) && !defined(__cplusplus)
    printf( "Traditional C Compiler!\n" );
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199409L
    printf( "C95 Compiler!\n" );
#endif
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
    printf( "C99 Compiler!\n" );
#endif
#ifdef __GNUC__
    printf( "GCC Compiler: %d!\n", __GNUC__ );
#endif
#ifdef __BORLANDC__
    printf( "Borland Compiler: %d!\n", __BORLANDC__ );
#endif
#ifdef _MSC_VER
    printf( "Microsoft Compiler: %d!\n", _MSC_VER );
#endif

#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_C95_WIDE_CHARACTER__
    printf( "support C95 wide character!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_BOOLEAN_TYPE__
    printf( "support bool!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_LONG_LONG_TYPE__
    printf( "support long long!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_INLINE_FUNCTION__
    printf( "support inline function!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_VARIABLE_LENGTH_ARRAY__
    printf( "support variable length array!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_INPUT_OUTPUT_SYSTEM__
    printf( "support input and output system!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_FILE_SYSTEM__
    printf( "support file system!\n" );
#endif
#ifdef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_STANDARD_MEMORY_FUNCTION__
    printf( "support standard memory function!\n" );
#endif

    printf( "\n\nMB_LEN_MAX = %d\n", MB_LEN_MAX );
    printf( "SIZE_MAX = %u\n", SIZE_MAX );
    printf( "size of bool = %u,  true = %d,  false = %d\n",
            sizeof(bool), true, false );
    printf( "size of ylib_word_t = %u\n", sizeof(ylib_word_t) );
    printf( "size of ylib_inner_t = %u\n", sizeof(ylib_inner_t) );

/*    test_algo();*/
/*    test_copytime();*/

    while( 1 )
    {
        int choice = 0;

        printf( "\n\n\nMain Menu:\n"  );
        printf( "  1: test dynamic memory array\n" );
        printf( "  2: test dynamic resource array\n" );
        printf( "  3: test chunk array\n" );
        printf( "  4: test doubly linked list\n" );
        printf( "  5: test singly linked list\n" );
        printf( "  6: test balanced binary search tree\n" );
        printf( "  7: test hash table\n" );
        printf( "  8: test string\n" );
        printf( "  0: exit\n" );

        scanf( "%d", &choice );

        switch( choice )
        {
            case 0:  goto EXIT;
            case 1:  test_dstring();  break;
            case 2:  test_dyrscarr();  break;
            case 3:  test_chkarray();  break;
            case 4:  test_dblnklst();  break;
            case 5:  test_sglnklst();  break;
            case 6:  test_tree();  break;
            case 7:  test_hashtable();  break;
            case 8:  test_string();  break;
            default: break;
        }
    }

EXIT:
    printf( "\n\npool allocate count: %u\n", get_pool_alloc_count() );
    printf( "pool deallocate count: %u\n", get_pool_dealloc_count() );
    pool_print();
    printf( "\npress any key to continue\n" );
    getchar();
    getchar();

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -