コード例 #1
0
ファイル: checkhelp.c プロジェクト: gstraube/c_exercises
/* Pick max <n> characters from string <start> around position <cur>.
 * Write those to <dst>.
 */
void showref(char *dst, const char *start, const char *cur, size_t n)
{
    if (strlen(start) > n-1 && (size_t) (cur - start) > (n / 2) + 1) {
        strncpy(dst, cur - (n/2) - 1, n-1);
    } else {
        strncpy(dst, start, n-1);
    }
    dst[n-1] = 0;
    remove_nonascii(dst);
}
コード例 #2
0
ファイル: test_source.c プロジェクト: aajarven/c
int test_product(char *buf, struct product *pr, const char *title, const char *code,
        int stock, double price) {
    if (pr->title == title || pr->title == NULL) {
        sprintf(buf, "Memory for title string not dynamically allocated");
        return 0;
    }
    if (strcmp(pr->title, title)) {
        remove_nonascii(pr->title);
        sprintf(buf, "Incorrect title. You: \"%s\", expected: \"%s\"",
                pr->title, title);
        return 0;
    }

    if (strlen(pr->code) > 7) {
        sprintf(buf, "Code field is too long. Max. length is 7 characters (plus '\\0')");
        return 0;
    }
    char codebuf[8];
    strncpy(codebuf, code, 7);
    codebuf[7] = 0;

    if (strcmp(pr->code, codebuf)) {
        remove_nonascii(pr->code);
        sprintf(buf, "Incorrect code. You: \"%s\", expected: \"%s\"",
                pr->code, codebuf);
        return 0;
    }
    if (pr->stock != stock) {
        sprintf(buf, "Incorrect stock. You: %d, expected: %d",
                pr->stock, stock);
        return 0;
    }
    if (pr->price != price) {
        sprintf(buf, "Incorrect price. You: %lf, expected: %lf",
                pr->price, price);
        return 0;
    }
    return 1;
}
コード例 #3
0
ファイル: test_source.c プロジェクト: noppud/c-course
END_TEST


START_TEST(test_make_lower)
{
    char strs[][4][20] = {{ "oNe", "TWO", "three", "Four" },
    { "ALPHA", "beta", "Gamma", "Theta" }};
    char ref[][4][20] = {{ "one", "two", "three", "four" },
    { "alpha", "beta", "gamma", "theta" }};

    const unsigned int num = 4;
    
    for (int k = 0; k < 2; k++) {
    char **arr = init_array();
    if(!arr) {
      fail("[Task 4.3.c] init_array returned NULL");
    }
    for (unsigned int i = 0; i < num; i++) {
        char **a2 = add_string(arr, strs[k][i]);
	if (!a2) {
	  free_strings(arr);
	  fail("[Task 4.3.c] add_string returned NULL");
	}
	arr = a2;
    }
    
    char buf[200];
    printref(buf, strs[k], num);
    
    make_lower(arr);
    
    for (unsigned int i = 0; i < num; i++) {
        if (strcmp(arr[i], ref[k][i])) {
            remove_nonascii(arr[i]);
            char buf2[200];
            sprintf(buf2, "[Task 4.3.c] After adding strings {%s}, array member %d is '%s', should be '%s'",
                    buf, i, arr[i], ref[k][i]);
            free_strings(arr);
            fail(buf2);
        }
    }
    free_strings(arr);
    }
}