Path: blob/main/crypto/krb5/src/lib/rpc/dyntest.c
105932 views
/*1* This file is a (rather silly) demonstration of the use of the2* C Dynamic Object library. It is a also reasonably thorough test3* of the library (except that it only tests it with one data size).4*5* There are no restrictions on this code; however, if you make any6* changes, I request that you document them so that I do not get7* credit or blame for your modifications.8*9* Written by Barr3y Jaspan, Student Information Processing Board (SIPB)10* and MIT-Project Athena, 1989.11*12* 2002-07-17 Moved here from util/dyn/test.c. Older changes described13* at end of file; for newer changes see ChangeLog.14*/1516#include <stdio.h>17#include <string.h>18#ifdef USE_DBMALLOC19#include <sys/stdtypes.h>20#include <malloc.h>21#endif22#include <stdlib.h>2324#include "dyn.h"2526static char random_string[] = "This is a random string.";27static char insert1[] = "This will be put at the beginning.";28static char insert2[] = "(parenthetical remark!) ";29static char insert3[] = " This follows the random string.";3031int32main(argc, argv)33/*@unused@*/int argc;34/*@unused@*/char **argv;35{36/*@-exitarg@*/37DynObject obj;38int i, s;39char d, *data;4041#ifdef _DEBUG_MALLOC_INC42union dbmalloptarg arg;43unsigned long hist1, hist2, o_size, c_size;44#endif4546#ifdef _DEBUG_MALLOC_INC47arg.i = 0;48dbmallopt(MALLOC_ZERO, &arg);49dbmallopt(MALLOC_REUSE, &arg);5051o_size = malloc_inuse(&hist1);52#endif5354/*@+matchanyintegral@*/55obj = DynCreate(sizeof(char), -8);56if (! obj) {57fprintf(stderr, "test: create failed.\n");58exit(1);59}6061if(DynDebug(obj, 1) != DYN_OK) {62fprintf(stderr, "test: setting paranoid failed.\n");63exit(1);64}65if(DynParanoid(obj, 1) != DYN_OK) {66fprintf(stderr, "test: setting paranoid failed.\n");67exit(1);68}697071if ((DynGet(obj, -5) != NULL) ||72(DynGet(obj, 0) != NULL) || (DynGet(obj, 1000) != NULL)) {73fprintf(stderr, "test: Get did not fail when it should have.\n");74exit(1);75}7677if (DynDelete(obj, -1) != DYN_BADINDEX ||78DynDelete(obj, 0) != DYN_BADINDEX ||79DynDelete(obj, 100) != DYN_BADINDEX) {80fprintf(stderr, "test: Delete did not fail when it should have.\n");81exit(1);82}8384printf("Size of empty object: %d\n", DynSize(obj));8586for (i=0; i<14; i++) {87d = (char) i;88if (DynAdd(obj, &d) != DYN_OK) {89fprintf(stderr, "test: Adding %d failed.\n", i);90exit(1);91}92}9394if (DynAppend(obj, random_string, strlen(random_string)+1) != DYN_OK) {95fprintf(stderr, "test: appending array failed.\n");96exit(1);97}9899if (DynDelete(obj, DynHigh(obj) / 2) != DYN_OK) {100fprintf(stderr, "test: deleting element failed.\n");101exit(1);102}103104if (DynDelete(obj, DynHigh(obj) * 2) == DYN_OK) {105fprintf(stderr, "test: delete should have failed here.\n");106exit(1);107}108109d = '\200';110if (DynAdd(obj, &d) != DYN_OK) {111fprintf(stderr, "test: Adding %d failed.\n", i);112exit(1);113}114115data = (char *) DynGet(obj, 0);116if(data == NULL) {117fprintf(stderr, "test: getting object 0 failed.\n");118exit(1);119}120s = DynSize(obj);121for (i=0; i < s; i++)122printf("Element %d is %d.\n", i, (int) data[i]);123124data = (char *) DynGet(obj, 13);125if(data == NULL) {126fprintf(stderr, "test: getting element 13 failed.\n");127exit(1);128}129printf("Element 13 is %d.\n", (int) *data);130131data = (char *) DynGet(obj, DynSize(obj));132if (data) {133fprintf(stderr, "DynGet did not return NULL when it should have.\n");134exit(1);135}136137data = DynGet(obj, 14);138if(data == NULL) {139fprintf(stderr, "test: getting element 13 failed.\n");140exit(1);141}142printf("This should be the random string: \"%s\"\n", data);143144if (DynInsert(obj, -1, "foo", 4) != DYN_BADINDEX ||145DynInsert(obj, DynSize(obj) + 1, "foo", 4) != DYN_BADINDEX ||146DynInsert(obj, 0, "foo", -1) != DYN_BADVALUE) {147fprintf(stderr, "DynInsert did not fail when it should have.\n");148exit(1);149}150151if (DynInsert(obj, DynSize(obj) - 2, insert3, strlen(insert3) +1521) != DYN_OK) {153fprintf(stderr, "DynInsert to end failed.\n");154exit(1);155}156157if (DynInsert(obj, 19, insert2, strlen(insert2)) != DYN_OK) {158fprintf(stderr, "DynInsert to middle failed.\n");159exit(1);160}161162if (DynInsert(obj, 0, insert1, strlen(insert1)+1) != DYN_OK) {163fprintf(stderr, "DynInsert to start failed.\n");164exit(1);165}166167data = DynGet(obj, 14 + strlen(insert1) + 1);168if (data == NULL) {169fprintf(stderr, "DynGet of 14+strelen(insert1) failed.\n");170exit(1);171172}173printf("A new random string: \"%s\"\n", data);174175data = DynGet(obj, 0);176if (data == NULL) {177fprintf(stderr, "DynGet of 0 failed.\n");178exit(1);179180}181printf("This was put at the beginning: \"%s\"\n", data);182183if(DynDestroy(obj) != DYN_OK) {184fprintf(stderr, "test: destroy failed.\n");185exit(1);186}187188#ifdef _DEBUG_MALLOC_INC189c_size = malloc_inuse(&hist2);190if (o_size != c_size) {191printf("\n\nIgnore a single unfreed malloc segment "192"(stdout buffer).\n\n");193malloc_list(2, hist1, hist2);194}195#endif196197printf("All tests pass\n");198199return 0;200}201202/* Old change log, as it relates to source code; build system stuff203discarded.2042052001-04-25 Ezra Peisach <[email protected]>206207* test.c: Always include stdlib.h208209* test.c: Check the return values of all library calls.2102112000-11-09 Ezra Peisach <[email protected]>212213* test.c: Include string,h, stdlib.h.214215*/216217218