ma_test.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,089 行 · 第 1/3 页

C
1,089
字号
            ptr_int    = (long) _fcalloc( NUM_EL, sizeof( int ) );
            chkmemavl = _memavl();
            ptr_double = (long) _fcalloc( NUM_EL, sizeof( double ) );
            break;
        case TYPE_BASED:
            if( more_debug ) {
                printf( "Testing: _bcalloc(), _bmsize()...\n" );
            }
            if( seg == _NULLSEG ) {
                result->status = TEST_NOSEG;
                if( more_debug ) noseg_lineno = __LINE__;
                return;
            }
            strcpy( result->funcname,"_bcalloc()" );
            ptr_char   = (long) _bcalloc( seg, NUM_EL, sizeof( char ) );
            ptr_int    = (long) _bcalloc( seg, NUM_EL, sizeof( int ) );
            chkmemavl = _memavl();
            ptr_double = (long) _bcalloc( seg, NUM_EL, sizeof( double ) );
            break;
#endif
        default:
            break;
    }
    if( ptr_char == _NUL && ( ptr_int != _NUL || ptr_double != _NUL ) ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[2] );
        _CRET();
    }
    if( ptr_int == _NUL && ptr_double != _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[3] );
        _CRET();
    }
    if( ptr_char == _NUL || ptr_int == _NUL ) {
        result->status = TEST_NOMEM;
        if( more_debug ) nomem_lineno = __LINE__;
        _CRET();
    }
#if !defined(__386__) && !defined(__AXP__) && (defined(__SMALL__) || defined(__MEDIUM__))
    if( type != TYPE_FAR && HACK_CAST ptr_double != HACK_CAST _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[14] );
        _CRET();
    } else if( type == TYPE_FAR && ptr_double == _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[16] );
        _CRET();
    }
#else       // 32-bit or large data model
    if( type == TYPE_BASED || type == TYPE_NEAR ) {
    #if !defined(__386__) && !defined(__AXP__)
        if( HACK_CAST ptr_double != HACK_CAST _NUL ) {
            result->status = TEST_FAIL;
            strcpy( result->msg, errmsg[14] );
            _CRET();
        }
    #endif
    } else if( ptr_double == _NUL ) {
        result->status = TEST_FAIL;
    #if !defined(__386__) && !defined(__AXP__) // Different error messages
        strcpy( result->msg, errmsg[16] );
    #else
        strcpy( result->msg, errmsg[17] );
    #endif
        _CRET();
    #if !defined(__386__) && !defined(__AXP__)
    } else if( _memavl() < chkmemavl ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[18] );
        _CRET();
    #endif
    }
#endif

    result->status = TEST_PASS;
    tracethisloop = AskTrace( result->funcname, __LINE__ );
    for( ctr = 0; ctr < NUM_EL && result->status == TEST_PASS; ++ctr ) {
        switch( type ) {
            case TYPE_DEFAULT:
                if( ((char *)ptr_char)[ctr] != 0 ||
                    ((int *)ptr_int)[ctr] != 0 ) {
                    result->status = TEST_FAIL;
                }
                break;
            case TYPE_NEAR:
                if( ((char __near *)ptr_char)[ctr] != 0 ||
                    ((int __near *)ptr_int)[ctr] != 0 ) {
                    result->status = TEST_FAIL;
                }
                break;
#if !defined(__386__) && !defined(__AXP__)
            case TYPE_FAR:
                if( ((char __far *)ptr_char)[ctr] != 0 ||
                    ((int __far *)ptr_int)[ctr] != 0 ) {
                    result->status = TEST_FAIL;
                }
                break;
            case TYPE_BASED:
                if( ((char __based( seg ) *)ptr_char)[ctr] != 0 ||
                    ((int __based( seg ) *)ptr_int)[ctr] != 0 ) {
                    result->status = TEST_FAIL;
                }
                break;
#endif
            default:
                break;
        }
        if( tracethisloop ) ShowDot( ctr );
    }
    if( tracethisloop )cprintf( "\r\nTrace done. No accessing problems.\r\n");
    if( result->status != TEST_PASS ) {
        strcpy( result->msg, errmsg[4] ); // didn't clear the memory
        _CRET();
    }
    size = NUM_EL;
    if( size % WSIZE != 0 ) {
        size += ( WSIZE - ( size % WSIZE ) );   // Align it to WSIZE
    }
    switch( type ) {
        case TYPE_DEFAULT:
            strcpy( result->funcname,"_msize()" );
            retsize = _msize( (char *)ptr_char );
            break;
        case TYPE_NEAR:
            strcpy( result->funcname,"_nmsize()" );
            retsize = _nmsize( (char __near *)ptr_char );
            break;
#if !defined(__386__) && !defined(__AXP__)
        case TYPE_FAR:
            strcpy( result->funcname,"_fmsize()" );
            retsize = _fmsize( (char __far *)ptr_char );
            break;
        case TYPE_BASED:
            strcpy( result->funcname,"_bmsize()" );
            retsize = _bmsize( seg, (char __based( seg ) *)ptr_char );
            break;
#endif
        default:
            break;
    }
    if( retsize < size || retsize > size + SIZE_MARGIN ) {
        result->status = TEST_FAIL;
        sprintf( result->msg, "%s. size = %u, returned = %u", errmsg[9],
                 size, retsize );
    }
    _CRET();
} // Test_calloc__msize() //

void Test_malloc_realloc__expand( test_result *result, int type )
{
    size_t      ctr, size, chkmemavl;
    int         f_pass;
    long        ptr_char    = NULL;
    long        ptr_int     = NULL;
    long        ptr_double  = NULL;
    long        tmp_ptr = NULL;

    switch( type ) {
        case TYPE_DEFAULT:
            if( more_debug ) {
                printf( "Testing: malloc(), realloc(), expand()...\n" );
            }
            strcpy( result->funcname,"malloc()" );
            ptr_char   = (long) malloc( NUM_EL * sizeof( char ) );
            ptr_int    = (long) malloc( NUM_EL * sizeof( int ) );
            chkmemavl = _memavl();
            ptr_double = (long) malloc( NUM_EL * sizeof( double ) );
            break;
        case TYPE_NEAR:
            if( more_debug ) {
                printf( "Testing: _nmalloc(), _nrealloc(), _nexpand()...\n" );
            }
            strcpy( result->funcname,"_nmalloc()" );
            ptr_char   = (long) _nmalloc( NUM_EL * sizeof( char ) );
            ptr_int    = (long) _nmalloc( NUM_EL * sizeof( int ) );
            ptr_double = (long) _nmalloc( NUM_EL * sizeof( double ) );
            break;
#if !defined(__386__) && !defined(__AXP__)
        case TYPE_FAR:
            if( more_debug ) {
                printf( "Testing: _fmalloc(), _frealloc(), _fexpand()...\n" );
            }
            strcpy( result->funcname,"_fmalloc()" );
            ptr_char   = (long) _fmalloc( NUM_EL * sizeof( char ) );
            ptr_int    = (long) _fmalloc( NUM_EL * sizeof( int ) );
            chkmemavl = _memavl();
            ptr_double = (long) _fmalloc( NUM_EL * sizeof( double ) );
            break;
        case TYPE_BASED:
            if( more_debug ) {
                printf( "Testing: _bmalloc(), _brealloc(), _bexpand()...\n" );
            }
            if( seg == _NULLSEG ) {
                result->status = TEST_NOSEG;
                if( more_debug ) noseg_lineno = __LINE__;
                return;
            }
            strcpy( result->funcname,"_bmalloc()" );
            ptr_char   = (long) _bmalloc( seg, NUM_EL * sizeof( char ) );
            ptr_int    = (long) _bmalloc( seg, NUM_EL * sizeof( int ) );
            chkmemavl = _memavl();
            ptr_double = (long) _bmalloc( seg, NUM_EL * sizeof( double ) );
            break;
#endif
        default:
            break;
    }
    if( ptr_char == _NUL && (ptr_int != _NUL || ptr_double !=_NUL) ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[2] );
        _CRET();
    }
    if( ptr_int == _NUL && ptr_double != _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[3] );
        _CRET();
    }
    if( ptr_char == _NUL || ptr_int == _NUL ) {
        result->status = TEST_NOMEM;
        if( more_debug ) nomem_lineno = __LINE__;
        _CRET();
    }
#if !defined(__386__) && !defined(__AXP__) && (defined(__SMALL__) || defined(__MEDIUM__))
    if( type != TYPE_FAR && HACK_CAST ptr_double != HACK_CAST _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[14] );
        _CRET();
    } else if( type == TYPE_FAR && ptr_double == _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[16] );
        _CRET();
    }
#else       // 32-bit or large data model
    if( type == TYPE_BASED || type == TYPE_NEAR ) {
    #if !defined(__386__) && !defined(__AXP__)
        if( HACK_CAST ptr_double != HACK_CAST _NUL ) {
            result->status = TEST_FAIL;
            strcpy( result->msg, errmsg[14] );
            _CRET();
        }
    #endif
    } else if( ptr_double == _NUL ) {
        result->status = TEST_FAIL;
    #if !defined(__386__) && !defined(__AXP__) // Different error messages
        strcpy( result->msg, errmsg[16] );
    #else
        strcpy( result->msg, errmsg[17] );
    #endif
        _CRET();
    #if !defined(__386__) && !defined(__AXP__)
    } else if( _memavl() < chkmemavl ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[18] );
        _CRET();
    #endif
    }
#endif

    result->status = TEST_PASS;
    tracethisloop = AskTrace( result->funcname, __LINE__ );
    for( ctr = 0; ctr < NUM_EL; ++ctr ) {
        switch( type ) {
            case TYPE_DEFAULT:
                ((char *)ptr_char)[ctr] = 6;
                ((int *)ptr_int)[ctr]   = 6;
                break;
            case TYPE_NEAR:
                ((char __near *)ptr_char)[ctr] = 6;
                ((int __near *)ptr_int)[ctr]   = 6;
                break;
#if !defined(__386__) && !defined(__AXP__)
            case TYPE_FAR:
                ((char __far *)ptr_char)[ctr] = 6;
                ((int __far *)ptr_int)[ctr]   = 6;
                break;
            case TYPE_BASED:
                ((char __based( seg ) *)ptr_char)[ctr] = 6;
                ((int __based( seg ) *)ptr_int)[ctr]   = 6;
                break;
#endif
            default:
                break;
        }
        if( tracethisloop ) ShowDot( ctr );
    }   // Make sure that array is accessible.
    if(tracethisloop) cprintf( "\r\nTrace done. No problems detected.\r\n" );

    // The following tests realloc() and _expand():
    size = ( NUM_EL >> 1) * sizeof( int ); // shrink by half
    switch( type ) {
        case TYPE_DEFAULT:
            strcpy( result->funcname, "realloc()" );
            tmp_ptr = (long)realloc( (int *)ptr_int, size );
            break;
        case TYPE_NEAR:
            strcpy( result->funcname, "_nrealloc()" );
            tmp_ptr = (long) _nrealloc((int __near*) ptr_int, size );
            break;
#if !defined(__386__) && !defined(__AXP__)
        case TYPE_FAR:
            strcpy( result->funcname, "_frealloc()" );
            tmp_ptr = (long) _frealloc( (int __far *) ptr_int, size );
            break;
        case TYPE_BASED:
            strcpy( result->funcname, "_brealloc()" );
            tmp_ptr = (long) _brealloc( seg,(int __based(seg)*)ptr_int,size);
            break;
#endif
        default:
            break;
    }
    if( tmp_ptr == _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[5] );
        _CRET();
    }

    ptr_int = tmp_ptr;
    f_pass = CheckContent( ptr_int, ( NUM_EL >> 1 ), type );
    if( !f_pass ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[6] ); // Content changed!
        _CRET();
    }   // realloc() done.

    size = NUM_EL * sizeof( int );      // Back to original size
    switch( type ) {
        case TYPE_DEFAULT:
            strcpy( result->funcname, "_expand()" );
            tmp_ptr = (long)_expand( (int *)ptr_int, size );
            break;
        case TYPE_NEAR:
            strcpy( result->funcname, "_nexpand()" );
            tmp_ptr = (long)_nexpand( (int __near *)ptr_int, size );
            break;
#if !defined(__386__) && !defined(__AXP__)
        case TYPE_FAR:
            strcpy( result->funcname, "_fexpand()" );
            tmp_ptr = (long)_fexpand( (int __far *)ptr_int, size );
            break;
        case TYPE_BASED:
            strcpy( result->funcname, "_bexpand()" );
            tmp_ptr = (long)_bexpand( seg, (int __based(seg)*)ptr_int, size );
            break;
#endif
        default:
            break;
    }
    if( tmp_ptr == _NUL ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[15] );
        _CRET();
    }

    f_pass = CheckContent( ptr_int, ( NUM_EL >> 1 ), type );
    if( !f_pass ) {
        result->status = TEST_FAIL;
        strcpy( result->msg, errmsg[6] ); // Content changed!
        _CRET();
    }   // realloc() done.

    for( ctr = ( NUM_EL >> 1 ); ctr < NUM_EL; ++ctr ) {
        switch( type ) {

⌨️ 快捷键说明

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