1*7fd79137SRobert Mustacchi /* 2*7fd79137SRobert Mustacchi * This file and its contents are supplied under the terms of the 3*7fd79137SRobert Mustacchi * Common Development and Distribution License ("CDDL"), version 1.0. 4*7fd79137SRobert Mustacchi * You may only use this file in accordance with the terms of version 5*7fd79137SRobert Mustacchi * 1.0 of the CDDL. 6*7fd79137SRobert Mustacchi * 7*7fd79137SRobert Mustacchi * A full copy of the text of the CDDL should have accompanied this 8*7fd79137SRobert Mustacchi * source. A copy of the CDDL is also available via the Internet at 9*7fd79137SRobert Mustacchi * http://www.illumos.org/license/CDDL. 10*7fd79137SRobert Mustacchi */ 11*7fd79137SRobert Mustacchi 12*7fd79137SRobert Mustacchi /* 13*7fd79137SRobert Mustacchi * Copyright 2015 Joyent, Inc. 14*7fd79137SRobert Mustacchi */ 15*7fd79137SRobert Mustacchi 16*7fd79137SRobert Mustacchi /* 17*7fd79137SRobert Mustacchi * mergeq testing routines 18*7fd79137SRobert Mustacchi */ 19*7fd79137SRobert Mustacchi 20*7fd79137SRobert Mustacchi #include <mergeq.h> 21*7fd79137SRobert Mustacchi #include <stdio.h> 22*7fd79137SRobert Mustacchi #include <errno.h> 23*7fd79137SRobert Mustacchi #include <string.h> 24*7fd79137SRobert Mustacchi #include <stdlib.h> 25*7fd79137SRobert Mustacchi 26*7fd79137SRobert Mustacchi const char * 27*7fd79137SRobert Mustacchi _umem_debug_init() 28*7fd79137SRobert Mustacchi { 29*7fd79137SRobert Mustacchi return ("default,verbose"); 30*7fd79137SRobert Mustacchi } 31*7fd79137SRobert Mustacchi 32*7fd79137SRobert Mustacchi const char * 33*7fd79137SRobert Mustacchi _umem_logging_init(void) 34*7fd79137SRobert Mustacchi { 35*7fd79137SRobert Mustacchi return ("fail,contents"); 36*7fd79137SRobert Mustacchi } 37*7fd79137SRobert Mustacchi 38*7fd79137SRobert Mustacchi void * 39*7fd79137SRobert Mustacchi mergeq_alloc(size_t size) 40*7fd79137SRobert Mustacchi { 41*7fd79137SRobert Mustacchi return (malloc(size)); 42*7fd79137SRobert Mustacchi } 43*7fd79137SRobert Mustacchi 44*7fd79137SRobert Mustacchi /*ARGSUSED*/ 45*7fd79137SRobert Mustacchi void 46*7fd79137SRobert Mustacchi mergeq_free(void *buf, size_t size) 47*7fd79137SRobert Mustacchi { 48*7fd79137SRobert Mustacchi free(buf); 49*7fd79137SRobert Mustacchi } 50*7fd79137SRobert Mustacchi 51*7fd79137SRobert Mustacchi static int 52*7fd79137SRobert Mustacchi mqt_int(void *first, void *second, void **outp, void *arg) 53*7fd79137SRobert Mustacchi { 54*7fd79137SRobert Mustacchi uintptr_t a, b, c; 55*7fd79137SRobert Mustacchi a = (uintptr_t)first; 56*7fd79137SRobert Mustacchi b = (uintptr_t)second; 57*7fd79137SRobert Mustacchi c = a + b; 58*7fd79137SRobert Mustacchi *outp = (void *)c; 59*7fd79137SRobert Mustacchi 60*7fd79137SRobert Mustacchi return (0); 61*7fd79137SRobert Mustacchi } 62*7fd79137SRobert Mustacchi 63*7fd79137SRobert Mustacchi static int 64*7fd79137SRobert Mustacchi mqt_append(void *first, void *second, void **outp, void *arg) 65*7fd79137SRobert Mustacchi { 66*7fd79137SRobert Mustacchi char *out; 67*7fd79137SRobert Mustacchi 68*7fd79137SRobert Mustacchi /* Yes, this leaks, don't worry about it for the test */ 69*7fd79137SRobert Mustacchi if (asprintf(&out, "%s%s", first, second) != -1) { 70*7fd79137SRobert Mustacchi *outp = out; 71*7fd79137SRobert Mustacchi return (0); 72*7fd79137SRobert Mustacchi } 73*7fd79137SRobert Mustacchi return (-1); 74*7fd79137SRobert Mustacchi } 75*7fd79137SRobert Mustacchi 76*7fd79137SRobert Mustacchi static int 77*7fd79137SRobert Mustacchi mqt_fatal(void *first, void *second, void **outp, void *arg) 78*7fd79137SRobert Mustacchi { 79*7fd79137SRobert Mustacchi return (-1); 80*7fd79137SRobert Mustacchi } 81*7fd79137SRobert Mustacchi 82*7fd79137SRobert Mustacchi /* 83*7fd79137SRobert Mustacchi * Test structures and cases. We really want mq_args to be a flexible array 84*7fd79137SRobert Mustacchi * member, but then we cant initialize it. Thus we set a fixed size number of 85*7fd79137SRobert Mustacchi * entries. 86*7fd79137SRobert Mustacchi */ 87*7fd79137SRobert Mustacchi typedef struct mq_test { 88*7fd79137SRobert Mustacchi const char *mq_desc; /* test description/name */ 89*7fd79137SRobert Mustacchi mergeq_proc_f *mq_proc; /* processing function */ 90*7fd79137SRobert Mustacchi int mq_rval; /* mergeq_merge return value */ 91*7fd79137SRobert Mustacchi int mq_uerr; /* user error, if any */ 92*7fd79137SRobert Mustacchi boolean_t mq_strcmp; /* use strcmp rather than == */ 93*7fd79137SRobert Mustacchi void *mq_result; /* expected result */ 94*7fd79137SRobert Mustacchi void **mq_args; /* argument array */ 95*7fd79137SRobert Mustacchi } mq_test_t; 96*7fd79137SRobert Mustacchi 97*7fd79137SRobert Mustacchi static void *mqt_empty_args[] = { NULL }; 98*7fd79137SRobert Mustacchi static void *mqt_single_args[] = { (void *)42, NULL }; 99*7fd79137SRobert Mustacchi static void *mqt_double_args[] = { (void *)42, (void *)27, NULL }; 100*7fd79137SRobert Mustacchi static void *mqt_wrap_args[] = { 101*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 102*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 103*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 104*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 105*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 106*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 107*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 108*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 109*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 110*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 111*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, NULL 112*7fd79137SRobert Mustacchi }; 113*7fd79137SRobert Mustacchi static void *mqt_grow_args[] = { 114*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 115*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 116*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 117*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 118*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 119*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 120*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 121*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 122*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 123*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 124*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 125*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 126*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 127*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 128*7fd79137SRobert Mustacchi (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, (void *)1, 129*7fd79137SRobert Mustacchi (void *)1, (void *)1, NULL 130*7fd79137SRobert Mustacchi }; 131*7fd79137SRobert Mustacchi static void *mqt_order_args[] = { "l", "e", "g", "e", "n", "d", " ", "o", "f", 132*7fd79137SRobert Mustacchi " ", "z", "e", "l", "d", "a", NULL }; 133*7fd79137SRobert Mustacchi 134*7fd79137SRobert Mustacchi 135*7fd79137SRobert Mustacchi static mq_test_t mq_tests[] = { 136*7fd79137SRobert Mustacchi { "empty", mqt_int, 0, 0, B_FALSE, NULL, mqt_empty_args }, 137*7fd79137SRobert Mustacchi { "single", mqt_int, 0, 0, B_FALSE, (void *)42, mqt_single_args }, 138*7fd79137SRobert Mustacchi { "double", mqt_int, 0, 0, B_FALSE, (void *)69, mqt_double_args }, 139*7fd79137SRobert Mustacchi { "wrap", mqt_int, 0, 0, B_FALSE, (void *)64, mqt_wrap_args }, 140*7fd79137SRobert Mustacchi { "grow", mqt_int, 0, 0, B_FALSE, (void *)92, mqt_grow_args }, 141*7fd79137SRobert Mustacchi { "fatal", mqt_fatal, MERGEQ_UERROR, -1, B_FALSE, NULL, 142*7fd79137SRobert Mustacchi mqt_double_args }, 143*7fd79137SRobert Mustacchi { "order", mqt_append, 0, 0, B_TRUE, "alegend of zeld", mqt_order_args } 144*7fd79137SRobert Mustacchi }; 145*7fd79137SRobert Mustacchi 146*7fd79137SRobert Mustacchi #define NMQ_TESTS (sizeof (mq_tests) / sizeof (mq_test_t)) 147*7fd79137SRobert Mustacchi 148*7fd79137SRobert Mustacchi static void 149*7fd79137SRobert Mustacchi mq_test_run(mergeq_t *mqp, mq_test_t *mqt) 150*7fd79137SRobert Mustacchi { 151*7fd79137SRobert Mustacchi int ret, err; 152*7fd79137SRobert Mustacchi void **itemp = mqt->mq_args; 153*7fd79137SRobert Mustacchi void *out; 154*7fd79137SRobert Mustacchi 155*7fd79137SRobert Mustacchi while (*itemp != NULL) { 156*7fd79137SRobert Mustacchi if ((ret = mergeq_add(mqp, *itemp)) != 0) { 157*7fd79137SRobert Mustacchi (void) fprintf(stderr, 158*7fd79137SRobert Mustacchi "test %s: failed to add item: %s\n", 159*7fd79137SRobert Mustacchi mqt->mq_desc, strerror(errno)); 160*7fd79137SRobert Mustacchi exit(1); 161*7fd79137SRobert Mustacchi } 162*7fd79137SRobert Mustacchi itemp++; 163*7fd79137SRobert Mustacchi } 164*7fd79137SRobert Mustacchi 165*7fd79137SRobert Mustacchi ret = mergeq_merge(mqp, mqt->mq_proc, NULL, &out, &err); 166*7fd79137SRobert Mustacchi if (ret != mqt->mq_rval) { 167*7fd79137SRobert Mustacchi (void) fprintf(stderr, "test %s: got incorrect rval. " 168*7fd79137SRobert Mustacchi "Expected %d, got %d\n", mqt->mq_desc, mqt->mq_rval, ret); 169*7fd79137SRobert Mustacchi exit(1); 170*7fd79137SRobert Mustacchi } 171*7fd79137SRobert Mustacchi 172*7fd79137SRobert Mustacchi if (ret == MERGEQ_UERROR && err != mqt->mq_uerr) { 173*7fd79137SRobert Mustacchi (void) fprintf(stderr, "test %s: got incorrect user error. " 174*7fd79137SRobert Mustacchi "Expected %d, got %d\n", mqt->mq_desc, mqt->mq_uerr, err); 175*7fd79137SRobert Mustacchi exit(1); 176*7fd79137SRobert Mustacchi } 177*7fd79137SRobert Mustacchi 178*7fd79137SRobert Mustacchi if (ret == 0) { 179*7fd79137SRobert Mustacchi if (mqt->mq_strcmp == B_TRUE && 180*7fd79137SRobert Mustacchi strcmp(out, mqt->mq_result) != 0) { 181*7fd79137SRobert Mustacchi (void) fprintf(stderr, "test %s: got unexpected " 182*7fd79137SRobert Mustacchi "result: %s, expected %s\n", mqt->mq_desc, out, 183*7fd79137SRobert Mustacchi mqt->mq_result); 184*7fd79137SRobert Mustacchi exit(1); 185*7fd79137SRobert Mustacchi } else if (mqt->mq_strcmp == B_FALSE && out != mqt->mq_result) { 186*7fd79137SRobert Mustacchi (void) fprintf(stderr, "test %s: got unexpected " 187*7fd79137SRobert Mustacchi "result: %p, expected %p\n", mqt->mq_desc, out, 188*7fd79137SRobert Mustacchi mqt->mq_result); 189*7fd79137SRobert Mustacchi exit(1); 190*7fd79137SRobert Mustacchi } 191*7fd79137SRobert Mustacchi } 192*7fd79137SRobert Mustacchi } 193*7fd79137SRobert Mustacchi 194*7fd79137SRobert Mustacchi int 195*7fd79137SRobert Mustacchi main(void) 196*7fd79137SRobert Mustacchi { 197*7fd79137SRobert Mustacchi int ret, i, t; 198*7fd79137SRobert Mustacchi mergeq_t *mqp; 199*7fd79137SRobert Mustacchi int nthreads[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, -1 }; 200*7fd79137SRobert Mustacchi 201*7fd79137SRobert Mustacchi for (t = 0; nthreads[t] != -1; t++) { 202*7fd79137SRobert Mustacchi printf("Beginning tests with %d threads\n", nthreads[t]); 203*7fd79137SRobert Mustacchi if ((ret = mergeq_init(&mqp, nthreads[t])) != 0) { 204*7fd79137SRobert Mustacchi fprintf(stderr, "failed to init mergeq: %s\n", 205*7fd79137SRobert Mustacchi strerror(errno)); 206*7fd79137SRobert Mustacchi return (1); 207*7fd79137SRobert Mustacchi } 208*7fd79137SRobert Mustacchi 209*7fd79137SRobert Mustacchi for (i = 0; i < NMQ_TESTS; i++) { 210*7fd79137SRobert Mustacchi mq_test_run(mqp, &mq_tests[i]); 211*7fd79137SRobert Mustacchi } 212*7fd79137SRobert Mustacchi 213*7fd79137SRobert Mustacchi mergeq_fini(mqp); 214*7fd79137SRobert Mustacchi } 215*7fd79137SRobert Mustacchi 216*7fd79137SRobert Mustacchi return (0); 217*7fd79137SRobert Mustacchi } 218