1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 *   Copyright (c) International Business Machines  Corp., 2002
4 *
5 *   01/02/2003	Port to LTP	avenkat@us.ibm.com
6 *   06/30/2001	Port to Linux	nsharoff@us.ibm.com
7 */
8
9/*\
10 * [Description]
11 *
12 * The testcase for test setting of buffer by check boundary conditions.
13 */
14
15#include <stdio.h>
16#include <string.h>
17#include <unistd.h>
18#include <stdlib.h>
19#include <errno.h>
20
21#include "tst_test.h"
22
23#define BSIZE 4096
24
25char buf[BSIZE];
26
27static void fill(void)
28{
29	register int i;
30
31	for (i = 0; i < BSIZE; i++)
32		buf[i] = 'a';
33}
34
35static int checkit(char *str)
36{
37	register int i = 0;
38
39	while (!*str++)
40		i++;
41
42	return i;
43}
44
45static void setup(void)
46{
47	fill();
48}
49
50static void verify_memset(void)
51{
52	register int i, j;
53	char *p = &buf[400];
54
55	for (i = 0; i < 200; i++) {
56		fill();
57		memset(p, 0, i);
58		if ((j = checkit(p)) != i) {
59			tst_res(TINFO, "Not enough zero bytes, wanted %d, got %d", i, j);
60			break;
61		}
62		if (!p[-1] || !p[i]) {
63			tst_res(TINFO, "Boundary error, clear of %d", i);
64			break;
65		}
66	}
67
68	if (i == 200)
69		tst_res(TPASS, "Test passed");
70	else
71		tst_res(TFAIL, "Test fails");
72}
73
74static struct tst_test test = {
75	.setup = setup,
76	.test_all = verify_memset,
77};
78