ctor22.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 65 行

C
65
字号
// 95/09/13 -- J.W.Welch        -- change name of string class

#include "fail.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *choices[] = {
    "aaaaa",
    "BBBBBBBBBBB",
    "cccCCCC",
    "xxcCCCC",
    "xx",
    "",
    NULL
};

struct StringClass {
    size_t len;
    char *mem;
    StringClass( char *p = "", size_t keep = 0 ) {
        mem = strdup(p);
        len = strlen(p)+1;
        if( keep && keep < len ) {
            mem[keep] = '\0';
            len = keep;
        }
    }
    ~StringClass(void) {
        free(mem);
    }
};

int should_be;

void Test_Print( StringClass *p, unsigned line )
{
    char **curr;
    for( curr = choices; *curr != NULL; ++curr ) {
        if( strcmp( p->mem, *curr ) == 0 ) break;
    }
    if( *curr == NULL ) {
        fail( line );
    }
    if(( curr - choices ) != should_be ) {
        fail( line );
    }
}

int main()
{
    StringClass v( choices[0] );
    should_be = 0;
    Test_Print( &v, __LINE__ );
    should_be = 1;
    Test_Print( &StringClass( choices[1] ), __LINE__ );
    should_be = 2;
    Test_Print( &StringClass( choices[2] ), __LINE__ );
    should_be = 4;
    Test_Print( &StringClass( choices[3], 2 ), __LINE__ );
    should_be = 5;
    Test_Print( &StringClass(), __LINE__ );
    _PASS;
}

⌨️ 快捷键说明

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