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