test_subr.c revision 168925
12343Scsgr/*- 22343Scsgr * Copyright (c) 2007 Michael Telahun Makonnen 32343Scsgr * All rights reserved. 42343Scsgr * 52343Scsgr * Redistribution and use in source and binary forms, with or without 62343Scsgr * modification, are permitted provided that the following conditions 72343Scsgr * are met: 82343Scsgr * 1. Redistributions of source code must retain the above copyright 92343Scsgr * notice, this list of conditions and the following disclaimer. 102343Scsgr * 2. Redistributions in binary form must reproduce the above copyright 112343Scsgr * notice, this list of conditions and the following disclaimer in the 122343Scsgr * documentation and/or other materials provided with the distribution. 132343Scsgr * 142343Scsgr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 152343Scsgr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 162343Scsgr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 172343Scsgr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 182343Scsgr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 192343Scsgr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 202343Scsgr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 212343Scsgr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 222343Scsgr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 232343Scsgr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 242343Scsgr * SUCH DAMAGE. 252343Scsgr * 262343Scsgr */ 272343Scsgr 282343Scsgr#include <sys/cdefs.h> 292343Scsgr__FBSDID("$FreeBSD: head/tools/regression/netinet6/inet6_rth/test_subr.c 168925 2007-04-21 11:23:33Z mtm $"); 302343Scsgr 312343Scsgr#include <sys/types.h> 322343Scsgr 332343Scsgr#include <stdio.h> 3495616Smarkm#include <stdlib.h> 352343Scsgr#include <string.h> 3695616Smarkm 3795616Smarkm#include "test_subr.h" 382343Scsgr 392343Scsgrint g_total; 4095616Smarkmint g_pass; 412343Scsgrint g_fail; 4227955Scharnierchar g_funcname[FUNCNAMESIZE]; 4395616Smarkmchar g_testdesc[LINESIZE]; 442343Scsgrchar g_errbuf[LINESIZE]; 452343Scsgr 4695616Smarkmvoid 472343Scsgrset_funcname(char *bufp, size_t bufsize) 482343Scsgr{ 4995616Smarkm strlcpy(g_funcname, bufp, 5095616Smarkm bufsize < FUNCNAMESIZE ? bufsize : FUNCNAMESIZE); 5195616Smarkm} 5295616Smarkm 5395616Smarkm/* 5495616Smarkm * desc is a NULL-terminated string. 5595616Smarkm */ 5695616Smarkmvoid 5795616Smarkmcheckptr(caddr_t expected, caddr_t got, const char *desc) 5895616Smarkm{ 592343Scsgr int len; 602343Scsgr int failed; 612343Scsgr char sbuf[LINESIZE]; 622343Scsgr 632343Scsgr memset((void *)sbuf, 0, LINESIZE); 642343Scsgr snprintf(g_testdesc, LINESIZE, desc); 652343Scsgr 6695616Smarkm failed = 1; 6795616Smarkm g_total++; 682343Scsgr if (got == expected) { 692343Scsgr len = snprintf(sbuf, LINESIZE, "ok"); 708874Srgrimes g_pass++; 712343Scsgr failed = 0; 722343Scsgr } else { 732343Scsgr len = snprintf(sbuf, LINESIZE, "not ok"); 742343Scsgr snprintf(g_errbuf, LINESIZE, " : Expected %#x, but got %#x", 752343Scsgr (unsigned int)expected, (unsigned int)got); 762343Scsgr g_fail++; 772343Scsgr } 782343Scsgr snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)", 792343Scsgr g_total, g_funcname, g_testdesc); 802343Scsgr printf(sbuf); 8195616Smarkm if (failed) 8295616Smarkm printf(g_errbuf); 832343Scsgr printf("\n"); 842343Scsgr fflush(NULL); 852343Scsgr memset((void *)g_errbuf, 0, LINESIZE); 8627955Scharnier memset((void *)g_testdesc, 0, LINESIZE); 8727955Scharnier} 882343Scsgr 892343Scsgrvoid 902343Scsgrcheckstr(const char *expected, const char *got, size_t explen, const char *desc) 912343Scsgr{ 922343Scsgr int len; 93121545Speter int failed; 9474462Salfred char sbuf[LINESIZE]; 952343Scsgr 962343Scsgr memset((void *)sbuf, 0, LINESIZE); 972343Scsgr snprintf(g_testdesc, LINESIZE, desc); 982343Scsgr 992343Scsgr failed = 1; 1002343Scsgr g_total++; 1012343Scsgr if (strncmp(expected, got, explen) == 0) { 1022343Scsgr len = snprintf(sbuf, LINESIZE, "ok"); 1032343Scsgr g_pass++; 104153090Sphilip failed = 0; 1052343Scsgr } else { 1062343Scsgr len = snprintf(sbuf, LINESIZE, "not ok"); 1072343Scsgr snprintf(g_errbuf, LINESIZE, 1088874Srgrimes " : Expected %s, but got %s", expected, got); 1092343Scsgr g_fail++; 1102343Scsgr } 1112343Scsgr snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)", 1122343Scsgr g_total, g_funcname, g_testdesc); 1132343Scsgr printf(sbuf); 1142343Scsgr if (failed) 1152343Scsgr printf(g_errbuf); 1162343Scsgr printf("\n"); 1172343Scsgr fflush(NULL); 1182343Scsgr memset((void *)g_errbuf, 0, LINESIZE); 1192343Scsgr memset((void *)g_testdesc, 0, LINESIZE); 1202343Scsgr} 1212343Scsgr 122153090Sphilipvoid 123153090Sphilipchecknum(int expected, int got, int cmp, const char *desc) 124153090Sphilip{ 1252343Scsgr int len; 126153090Sphilip int pass; 1272343Scsgr int failed; 128153090Sphilip char sbuf[LINESIZE]; 129153090Sphilip 130153090Sphilip memset((void *)sbuf, 0, LINESIZE); 131153090Sphilip snprintf(g_testdesc, LINESIZE, desc); 132153090Sphilip 133153090Sphilip failed = 1; 134153090Sphilip pass = 0; 1352343Scsgr g_total++; 136153090Sphilip switch(cmp) { 137153090Sphilip case 0: 138153090Sphilip pass = (got == expected) ? 1 : 0; 139153090Sphilip break; 140153090Sphilip case 1: 141153090Sphilip pass = (got > expected) ? 1 : 0; 142153090Sphilip break; 14312445Swpaul case -1: 1442343Scsgr pass = (got < expected) ? 1 : 0; 14512445Swpaul break; 14612445Swpaul } 1472343Scsgr if (pass != 0) { 1482343Scsgr len = snprintf(sbuf, LINESIZE, "ok"); 1492343Scsgr g_pass++; 1502343Scsgr failed = 0; 1512343Scsgr } else { 1522343Scsgr len = snprintf(sbuf, LINESIZE, "not ok"); 1532343Scsgr snprintf(g_errbuf, LINESIZE, 1542343Scsgr " : Expected %d, but got %d", expected, got); 1552343Scsgr g_fail++; 15685039Sfenner } 15785039Sfenner snprintf(sbuf + len, LINESIZE - len, " %d - %s (%s)", 1582343Scsgr g_total, g_funcname, g_testdesc); 1592343Scsgr printf(sbuf); 1602343Scsgr if (failed) 1612343Scsgr printf(g_errbuf); 16234993Sdanny printf("\n"); 1632343Scsgr fflush(NULL); 1642343Scsgr memset((void *)g_errbuf, 0, LINESIZE); 1652343Scsgr memset((void *)g_testdesc, 0, LINESIZE); 1662343Scsgr} 1672343Scsgr