1/* $NetBSD: t_dict.c,v 1.4 2011/02/12 23:21:33 christos Exp $ */ 2 3/* Copyright (c) 2010 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Mateusz Kocielski. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the NetBSD 20 * Foundation, Inc. and its contributors. 21 * 4. Neither the name of The NetBSD Foundation nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37#include <sys/cdefs.h> 38__RCSID("$NetBSD: t_dict.c,v 1.4 2011/02/12 23:21:33 christos Exp $"); 39 40#include <atf-c.h> 41#include <stdio.h> 42 43#include "dict.h" 44 45/* src/dict.c test cases */ 46 47/* saslc__dict_create() */ 48ATF_TC(t_saslc__dict_create); 49ATF_TC_HEAD(t_saslc__dict_create, tc) 50{ 51 52 atf_tc_set_md_var(tc, "descr", "saslc__dict_create() tests"); 53} 54ATF_TC_BODY(t_saslc__dict_create, tc) 55{ 56 57 saslc__dict_t *dict; 58 ATF_REQUIRE(dict = saslc__dict_create()); 59 saslc__dict_destroy(dict); 60} 61 62/* saslc__dict_insert() */ 63ATF_TC(t_saslc__dict_insert); 64ATF_TC_HEAD(t_saslc__dict_insert, tc) 65{ 66 67 atf_tc_set_md_var(tc, "descr", "saslc__dict_insert() tests"); 68} 69ATF_TC_BODY(t_saslc__dict_insert, tc) 70{ 71 72 saslc__dict_t *dict; 73 ATF_REQUIRE(dict = saslc__dict_create()); 74 ATF_CHECK_EQ(saslc__dict_insert(dict, "foo", "bar"), DICT_OK); 75 ATF_CHECK_EQ(saslc__dict_insert(dict, "bar", "blah"), DICT_OK); 76 ATF_CHECK_EQ(saslc__dict_insert(dict, " ", "bar"), DICT_KEYINVALID); 77 ATF_CHECK_EQ(saslc__dict_insert(dict, NULL, NULL), DICT_KEYINVALID); 78 ATF_CHECK_EQ(saslc__dict_insert(dict, "a", NULL), DICT_VALBAD); 79 ATF_CHECK_EQ(saslc__dict_insert(dict, 80 "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890", 81 "zero"), DICT_OK); 82 ATF_CHECK_EQ(saslc__dict_insert(dict, "a", "b"), DICT_OK); 83 ATF_CHECK_EQ(saslc__dict_insert(dict, "a", "c"), DICT_KEYEXISTS); 84 ATF_CHECK_EQ(saslc__dict_insert(dict, "foo", "bar"), DICT_KEYEXISTS); 85 ATF_CHECK_EQ(saslc__dict_insert(dict, "&^#%$#", "bad"), DICT_KEYINVALID); 86 saslc__dict_destroy(dict); 87} 88 89/* saslc__dict_remove() */ 90ATF_TC(t_saslc__dict_remove); 91ATF_TC_HEAD(t_saslc__dict_remove, tc) 92{ 93 94 atf_tc_set_md_var(tc, "descr", "saslc__dict_remove() tests"); 95} 96ATF_TC_BODY(t_saslc__dict_remove, tc) 97{ 98 99 saslc__dict_t *dict; 100 ATF_REQUIRE(dict = saslc__dict_create()); 101 ATF_CHECK_EQ(saslc__dict_remove(dict, "BAR"), DICT_KEYNOTFOUND); 102 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo", "bar"), DICT_OK); 103 ATF_CHECK_EQ(saslc__dict_remove(dict, "BAR"), DICT_KEYNOTFOUND); 104 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "BAR", "bar"), DICT_OK); 105 ATF_CHECK_EQ(saslc__dict_remove(dict, "BAR"), DICT_OK); 106 ATF_CHECK_EQ(saslc__dict_remove(dict, "BAR"), DICT_KEYNOTFOUND); 107 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "BAR", "bar"), DICT_OK); 108 ATF_CHECK_EQ(saslc__dict_remove(dict, "foo"), DICT_OK); 109 ATF_CHECK_EQ(saslc__dict_remove(dict, "BAR"), DICT_OK); 110 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo", "bar"), DICT_OK); 111 ATF_CHECK_EQ(saslc__dict_remove(dict, "foo"), DICT_OK); 112 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo1", "bar"), DICT_OK); 113 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo2", "bar"), DICT_OK); 114 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo3", "bar"), DICT_OK); 115 ATF_CHECK_EQ(saslc__dict_remove(dict, "foo2"), DICT_OK); 116 ATF_CHECK_EQ(saslc__dict_remove(dict, "foo1"), DICT_OK); 117 ATF_CHECK_EQ(saslc__dict_remove(dict, "foo3"), DICT_OK); 118 ATF_CHECK_EQ(saslc__dict_remove(dict, "foo3"), DICT_KEYNOTFOUND); 119 saslc__dict_destroy(dict); 120} 121 122/* saslc__dict_get() */ 123ATF_TC(t_saslc__dict_get); 124ATF_TC_HEAD(t_saslc__dict_get, tc) 125{ 126 127 atf_tc_set_md_var(tc, "descr", "saslc__dict_get() tests"); 128} 129ATF_TC_BODY(t_saslc__dict_get, tc) 130{ 131 132 saslc__dict_t *dict; 133 ATF_REQUIRE(dict = saslc__dict_create()); 134 ATF_CHECK_EQ(saslc__dict_get(dict, "BAR"), NULL); 135 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo1", "bar1"), DICT_OK); 136 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo2", "bar2"), DICT_OK); 137 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo3", "bar3"), DICT_OK); 138 ATF_CHECK_STREQ(saslc__dict_get(dict, "foo1"), "bar1"); 139 ATF_CHECK_STREQ(saslc__dict_get(dict, "foo2"), "bar2"); 140 ATF_CHECK_STREQ(saslc__dict_get(dict, "foo3"), "bar3"); 141 ATF_CHECK_EQ(saslc__dict_get(dict, "foo4"), NULL); 142 ATF_REQUIRE_EQ(saslc__dict_remove(dict, "foo2"), DICT_OK); 143 ATF_CHECK_STREQ(saslc__dict_get(dict, "foo1"), "bar1"); 144 ATF_CHECK_EQ(saslc__dict_get(dict, "foo2"), NULL); 145 ATF_CHECK_STREQ(saslc__dict_get(dict, "foo3"), "bar3"); 146 ATF_REQUIRE_EQ(saslc__dict_remove(dict, "foo1"), DICT_OK); 147 ATF_REQUIRE_EQ(saslc__dict_remove(dict, "foo3"), DICT_OK); 148 ATF_CHECK_EQ(saslc__dict_get(dict, "foo2"), NULL); 149 saslc__dict_destroy(dict); 150} 151 152/* saslc__dict_get_len() */ 153ATF_TC(t_saslc__dict_get_len); 154ATF_TC_HEAD(t_saslc__dict_get_len, tc) 155{ 156 157 atf_tc_set_md_var(tc, "descr", "saslc__dict_get_len() tests"); 158} 159ATF_TC_BODY(t_saslc__dict_get_len, tc) 160{ 161 162 saslc__dict_t *dict; 163 ATF_REQUIRE(dict = saslc__dict_create()); 164 ATF_CHECK_EQ(saslc__dict_get_len(dict, "BAR"), 0); 165 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo1", "1"), DICT_OK); 166 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo2", "1234567890"), DICT_OK); 167 ATF_REQUIRE_EQ(saslc__dict_insert(dict, "foo3", "12345678901234567890"), DICT_OK); 168 ATF_CHECK_EQ(saslc__dict_get_len(dict, "foo4"), 0); 169 ATF_CHECK_EQ(saslc__dict_get_len(dict, "foo1"), 1); 170 ATF_CHECK_EQ(saslc__dict_get_len(dict, "foo2"), 10); 171 ATF_CHECK_EQ(saslc__dict_get_len(dict, "foo3"), 20); 172 saslc__dict_destroy(dict); 173} 174 175ATF_TP_ADD_TCS(tp) 176{ 177 /* constructors and destructors */ 178 ATF_TP_ADD_TC(tp, t_saslc__dict_create); 179 180 /* modifiers */ 181 ATF_TP_ADD_TC(tp, t_saslc__dict_insert); 182 ATF_TP_ADD_TC(tp, t_saslc__dict_remove); 183 184 /* getters */ 185 ATF_TP_ADD_TC(tp, t_saslc__dict_get); 186 ATF_TP_ADD_TC(tp, t_saslc__dict_get_len); 187 188 return atf_no_error(); 189} 190