1248590Smm/* 2248590Smm * Copyright (c) 2003-2012 Tim Kientzle 3248590Smm * Copyright (c) 2012 Andres Mejia 4248590Smm * All rights reserved. 5248590Smm * 6248590Smm * Redistribution and use in source and binary forms, with or without 7248590Smm * modification, are permitted provided that the following conditions 8248590Smm * are met: 9248590Smm * 1. Redistributions of source code must retain the above copyright 10248590Smm * notice, this list of conditions and the following disclaimer. 11248590Smm * 2. Redistributions in binary form must reproduce the above copyright 12248590Smm * notice, this list of conditions and the following disclaimer in the 13248590Smm * documentation and/or other materials provided with the distribution. 14248590Smm * 15248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18248590Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25248590Smm */ 26248590Smm 27248590Smm#include "test_utils.h" 28248590Smm 29248590Smm#include <stdlib.h> 30248590Smm#include <string.h> 31248590Smm 32248590Smm/* Filter tests against a glob pattern. Returns non-zero if test matches 33248590Smm * pattern, zero otherwise. A '^' at the beginning of the pattern negates 34248590Smm * the return values (i.e. returns zero for a match, non-zero otherwise. 35248590Smm */ 36248590Smmstatic int 37248590Smmtest_filter(const char *pattern, const char *test) 38248590Smm{ 39248590Smm int retval = 0; 40248590Smm int negate = 0; 41248590Smm const char *p = pattern; 42248590Smm const char *t = test; 43248590Smm 44248590Smm if (p[0] == '^') 45248590Smm { 46248590Smm negate = 1; 47248590Smm p++; 48248590Smm } 49248590Smm 50248590Smm while (1) 51248590Smm { 52248590Smm if (p[0] == '\\') 53248590Smm p++; 54248590Smm else if (p[0] == '*') 55248590Smm { 56248590Smm while (p[0] == '*') 57248590Smm p++; 58248590Smm if (p[0] == '\\') 59248590Smm p++; 60248590Smm if ((t = strchr(t, p[0])) == 0) 61248590Smm break; 62248590Smm } 63248590Smm if (p[0] != t[0]) 64248590Smm break; 65248590Smm if (p[0] == '\0') { 66248590Smm retval = 1; 67248590Smm break; 68248590Smm } 69248590Smm p++; 70248590Smm t++; 71248590Smm } 72248590Smm 73248590Smm return (negate) ? !retval : retval; 74248590Smm} 75248590Smm 76248590Smmint get_test_set(int *test_set, int limit, const char *test, 77248590Smm struct test_list_t *tests) 78248590Smm{ 79248590Smm int start, end; 80248590Smm int idx = 0; 81248590Smm 82248590Smm if (test == NULL) { 83248590Smm /* Default: Run all tests. */ 84248590Smm for (;idx < limit; idx++) 85248590Smm test_set[idx] = idx; 86248590Smm return (limit); 87248590Smm } 88248590Smm if (*test >= '0' && *test <= '9') { 89248590Smm const char *vp = test; 90248590Smm start = 0; 91248590Smm while (*vp >= '0' && *vp <= '9') { 92248590Smm start *= 10; 93248590Smm start += *vp - '0'; 94248590Smm ++vp; 95248590Smm } 96248590Smm if (*vp == '\0') { 97248590Smm end = start; 98248590Smm } else if (*vp == '-') { 99248590Smm ++vp; 100248590Smm if (*vp == '\0') { 101248590Smm end = limit - 1; 102248590Smm } else { 103248590Smm end = 0; 104248590Smm while (*vp >= '0' && *vp <= '9') { 105248590Smm end *= 10; 106248590Smm end += *vp - '0'; 107248590Smm ++vp; 108248590Smm } 109248590Smm } 110248590Smm } else 111248590Smm return (-1); 112248590Smm if (start < 0 || end >= limit || start > end) 113248590Smm return (-1); 114248590Smm while (start <= end) 115248590Smm test_set[idx++] = start++; 116248590Smm } else { 117248590Smm for (start = 0; start < limit; ++start) { 118248590Smm const char *name = tests[start].name; 119248590Smm if (test_filter(test, name)) 120248590Smm test_set[idx++] = start; 121248590Smm } 122248590Smm } 123248590Smm return ((idx == 0)?-1:idx); 124248590Smm} 125