Path: blob/main/tools/regression/netinet6/inet6_rth/test_subr.c
39491 views
/*-1* Copyright (c) 2007 Michael Telahun Makonnen2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627#include <sys/types.h>2829#include <stdio.h>30#include <stdlib.h>31#include <string.h>3233#include "test_subr.h"3435int g_total;36int g_pass;37int g_fail;38char g_funcname[FUNCNAMESIZE];39char g_testdesc[LINESIZE];40char g_errbuf[LINESIZE];4142void43set_funcname(char *bufp, size_t bufsize)44{45strlcpy(g_funcname, bufp,46bufsize < FUNCNAMESIZE ? bufsize : FUNCNAMESIZE);47}4849/*50* desc is a NULL-terminated string.51*/52void53checkptr(caddr_t expected, caddr_t got, const char *desc)54{55int len;56int failed;57char sbuf[LINESIZE];5859memset((void *)sbuf, 0, LINESIZE);60snprintf(g_testdesc, LINESIZE, desc);6162failed = 1;63g_total++;64if (got == expected) {65len = snprintf(sbuf, LINESIZE, "ok");66g_pass++;67failed = 0;68} else {69len = snprintf(sbuf, LINESIZE, "not ok");70snprintf(g_errbuf, LINESIZE, " : Expected %#x, but got %#x",71(unsigned int)expected, (unsigned int)got);72g_fail++;73}74snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",75g_total, g_funcname, g_testdesc);76printf(sbuf);77if (failed)78printf(g_errbuf);79printf("\n");80fflush(NULL);81memset((void *)g_errbuf, 0, LINESIZE);82memset((void *)g_testdesc, 0, LINESIZE);83}8485void86checkstr(const char *expected, const char *got, size_t explen, const char *desc)87{88int len;89int failed;90char sbuf[LINESIZE];9192memset((void *)sbuf, 0, LINESIZE);93snprintf(g_testdesc, LINESIZE, desc);9495failed = 1;96g_total++;97if (strncmp(expected, got, explen) == 0) {98len = snprintf(sbuf, LINESIZE, "ok");99g_pass++;100failed = 0;101} else {102len = snprintf(sbuf, LINESIZE, "not ok");103snprintf(g_errbuf, LINESIZE,104" : Expected %s, but got %s", expected, got);105g_fail++;106}107snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",108g_total, g_funcname, g_testdesc);109printf(sbuf);110if (failed)111printf(g_errbuf);112printf("\n");113fflush(NULL);114memset((void *)g_errbuf, 0, LINESIZE);115memset((void *)g_testdesc, 0, LINESIZE);116}117118void119checknum(int expected, int got, int cmp, const char *desc)120{121int len;122int pass;123int failed;124char sbuf[LINESIZE];125126memset((void *)sbuf, 0, LINESIZE);127snprintf(g_testdesc, LINESIZE, desc);128129failed = 1;130pass = 0;131g_total++;132switch(cmp) {133case 0:134pass = (got == expected) ? 1 : 0;135break;136case 1:137pass = (got > expected) ? 1 : 0;138break;139case -1:140pass = (got < expected) ? 1 : 0;141break;142}143if (pass != 0) {144len = snprintf(sbuf, LINESIZE, "ok");145g_pass++;146failed = 0;147} else {148len = snprintf(sbuf, LINESIZE, "not ok");149snprintf(g_errbuf, LINESIZE,150" : Expected %d, but got %d", expected, got);151g_fail++;152}153snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)",154g_total, g_funcname, g_testdesc);155printf(sbuf);156if (failed)157printf(g_errbuf);158printf("\n");159fflush(NULL);160memset((void *)g_errbuf, 0, LINESIZE);161memset((void *)g_testdesc, 0, LINESIZE);162}163164165