source: web/old/remctl-2.14/tests/portable/strlcpy-t.c @ f6f3e91

web
Last change on this file since f6f3e91 was f6f3e91, checked in by Jessica B. Hamrick <jhamrick@…>, 15 years ago

Preserve directory hierarchy (not sure what happened to it)

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*
2 * strlcpy test suite.
3 *
4 * Written by Russ Allbery <rra@stanford.edu>
5 * Copyright 2009 Board of Trustees, Leland Stanford Jr. University
6 * Copyright (c) 2004, 2005, 2006
7 *     by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
9 *     2002, 2003 by The Internet Software Consortium and Rich Salz
10 *
11 * See LICENSE for licensing terms.
12 */
13
14#include <config.h>
15#include <portable/system.h>
16
17#include <tests/tap/basic.h>
18
19size_t test_strlcpy(char *, const char *, size_t);
20
21
22int
23main(void)
24{
25    char buffer[10];
26
27    plan(23);
28
29    is_int(3, test_strlcpy(buffer, "foo", sizeof(buffer)), "simple strlcpy");
30    is_string("foo", buffer, "...result is correct");
31    is_int(9, test_strlcpy(buffer, "hello wor", sizeof(buffer)),
32           "strlcpy exact length of buffer");
33    is_string("hello wor", buffer, "...result is correct");
34    is_int(10, test_strlcpy(buffer, "world hell", sizeof(buffer)),
35           "strlcpy one more than buffer length");
36    is_string("world hel", buffer, "...result is correct");
37    ok(buffer[9] == '\0', "...buffer is nul-terminated");
38    is_int(11, test_strlcpy(buffer, "hello world", sizeof(buffer)),
39           "strlcpy more than buffer length");
40    is_string("hello wor", buffer, "...result is correct");
41    ok(buffer[9] == '\0', "...buffer is nul-terminated");
42
43    /* Make sure that with a size of 0, the destination isn't changed. */
44    is_int(3, test_strlcpy(buffer, "foo", 0), "buffer unchanged if size 0");
45    is_string("hello wor", buffer, "...contents still the same");
46
47    /* Now play with empty strings. */
48    is_int(0, test_strlcpy(buffer, "", 0), "copy empty string with size 0");
49    is_string("hello wor", buffer, "...buffer unchanged");
50    is_int(0, test_strlcpy(buffer, "", sizeof(buffer)),
51           "copy empty string into full buffer");
52    is_string("", buffer, "...buffer now empty string");
53    is_int(3, test_strlcpy(buffer, "foo", 2),
54           "copy string into buffer of size 2");
55    is_string("f", buffer, "...got one character");
56    ok(buffer[1] == '\0', "...buffer is nul-terminated");
57    is_int(0, test_strlcpy(buffer, "", 1),
58           "copy empty string into buffer of size 1");
59    ok(buffer[0] == '\0', "...buffer is empty string");
60
61    /* Finally, check using strlcpy as strlen. */
62    is_int(3, test_strlcpy(NULL, "foo", 0), "use strlcpy as strlen");
63    is_int(11, test_strlcpy(NULL, "hello world", 0), "...again");
64
65    return 0;
66}
Note: See TracBrowser for help on using the repository browser.