1 # $NetBSD: t_db.sh,v 1.6 2015/11/18 18:35:35 christos Exp $
3 # Copyright (c) 2008 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
30 echo $(atf_get_srcdir)/h_db
35 echo $(atf_get_srcdir)/h_lfsr
40 if [ -f /usr/share/dict/words ]; then
41 echo /usr/share/dict/words
42 elif [ -f /usr/dict/words ]; then
45 atf_fail "no dictionary found"
52 if [ -f /usr/share/dict/words ]; then
53 echo /usr/share/dict/words
56 atf_skip "Test requires dict/words"
61 SEVEN_SEVEN="abcdefg|abcdefg|abcdefg|abcdefg|abcdefg|abcdefg|abcdefg"
63 atf_test_case small_btree
67 "Checks btree database using small keys and small data" \
68 "pairs: takes the first hundred entries in the dictionary," \
69 "and makes them be key/data pairs."
73 TMPDIR="$(pwd)/db_dir"; export TMPDIR
78 for i in `sed 200q $(dict)`; do
86 atf_check -o file:exp "$(prog_db)" btree in
89 atf_test_case small_hash
93 "Checks hash database using small keys and small data" \
94 "pairs: takes the first hundred entries in the dictionary," \
95 "and makes them be key/data pairs."
99 TMPDIR="$(pwd)/db_dir"; export TMPDIR
102 sed 200q $(dict) >exp
104 for i in `sed 200q $(dict)`; do
112 atf_check -o file:exp "$(prog_db)" hash in
115 atf_test_case small_recno
119 "Checks recno database using small keys and small data" \
120 "pairs: takes the first hundred entries in the dictionary," \
121 "and makes them be key/data pairs."
125 TMPDIR="$(pwd)/db_dir"; export TMPDIR
128 sed 200q $(dict) >exp
133 printf("p\nk%d\nd%s\ng\nk%d\n", i, $0, i);
136 atf_check -o file:exp "$(prog_db)" recno in
139 atf_test_case medium_btree
143 "Checks btree database using small keys and medium" \
144 "data pairs: takes the first 200 entries in the" \
145 "dictionary, and gives them each a medium size data entry."
149 TMPDIR="$(pwd)/db_dir"; export TMPDIR
152 mdata=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
154 awk '{ for (i = 1; i < 201; ++i) print $0 }' >exp
156 for i in $(sed 200q $(dict)); do
164 atf_check -o file:exp "$(prog_db)" btree in
167 atf_test_case medium_hash
171 "Checks hash database using small keys and medium" \
172 "data pairs: takes the first 200 entries in the" \
173 "dictionary, and gives them each a medium size data entry."
177 TMPDIR="$(pwd)/db_dir"; export TMPDIR
180 mdata=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
182 awk '{ for (i = 1; i < 201; ++i) print $0 }' >exp
184 for i in $(sed 200q $(dict)); do
192 atf_check -o file:exp "$(prog_db)" hash in
195 atf_test_case medium_recno
199 "Checks recno database using small keys and medium" \
200 "data pairs: takes the first 200 entries in the" \
201 "dictionary, and gives them each a medium size data entry."
205 TMPDIR="$(pwd)/db_dir"; export TMPDIR
208 mdata=abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
210 awk '{ for (i = 1; i < 201; ++i) print $0 }' >exp
213 awk '{ for (i = 1; i < 201; ++i)
214 printf("p\nk%d\nd%s\ng\nk%d\n", i, $0, i);
217 atf_check -o file:exp "$(prog_db)" recno in
220 atf_test_case big_btree
224 "Checks btree database using small keys and big data" \
225 "pairs: inserts the programs in /bin with their paths" \
230 TMPDIR="$(pwd)/db_dir"; export TMPDIR
233 (find /bin -type f -print | xargs cat) >exp
235 for psize in 512 16384 65536; do
236 echo "checking page size: $psize"
238 for i in `find /bin -type f -print`; do
246 atf_check "$(prog_db)" -o out btree in
247 cmp -s exp out || atf_fail "test failed for page size: $psize"
251 atf_test_case big_hash
255 "Checks hash database using small keys and big data" \
256 "pairs: inserts the programs in /bin with their paths" \
261 TMPDIR="$(pwd)/db_dir"; export TMPDIR
264 (find /bin -type f -print | xargs cat) >exp
266 for i in `find /bin -type f -print`; do
274 atf_check "$(prog_db)" -o out hash in
275 cmp -s exp out || atf_fail "test failed"
278 atf_test_case big_recno
282 "Checks recno database using small keys and big data" \
283 "pairs: inserts the programs in /bin with their paths" \
288 TMPDIR="$(pwd)/db_dir"; export TMPDIR
291 (find /bin -type f -print | xargs cat) >exp
293 find /bin -type f -print |
296 printf("p\nk%d\nD%s\ng\nk%d\n", i, $0, i);
299 for psize in 512 16384 65536; do
300 echo "checking page size: $psize"
302 atf_check "$(prog_db)" -o out recno in
303 cmp -s exp out || atf_fail "test failed for page size: $psize"
307 atf_test_case random_recno
310 atf_set "descr" "Checks recno database using random entries"
314 TMPDIR="$(pwd)/db_dir"; export TMPDIR
319 for (i = 37; i <= 37 + 88 * 17; i += 17) {
321 s = substr($0, 1, i % 41);
324 printf("input key %d: %s\n", i, s);
326 for (i = 1; i <= 15; ++i) {
328 s = substr($0, 1, i % 41);
331 printf("input key %d: %s\n", i, s);
333 for (i = 19234; i <= 19234 + 61 * 27; i += 27) {
335 s = substr($0, 1, i % 41);
338 printf("input key %d: %s\n", i, s);
349 printf("p\nk%d\nd%s\n", i, $0);
350 if (i == 19234 + 61 * 27)
352 if (i == 37 + 88 * 17) {
355 } else if (i == 15) {
362 for (i = 37; i <= 37 + 88 * 17; i += 17)
363 printf("g\nk%d\n", i);
364 for (i = 1; i <= 15; ++i)
365 printf("g\nk%d\n", i);
366 for (i = 19234; i <= 19234 + 61 * 27; i += 27)
367 printf("g\nk%d\n", i);
370 atf_check -o file:exp "$(prog_db)" recno in
373 atf_test_case reverse_recno
376 atf_set "descr" "Checks recno database using reverse order entries"
380 TMPDIR="$(pwd)/db_dir"; export TMPDIR
385 for (i = 1500; i; --i) {
387 s = substr($0, 1, i % 34);
390 printf("input key %d: %s\n", i, s);
400 printf("p\nk%d\nd%s\n", i, $0);
404 for (i = 1500; i; --i)
405 printf("g\nk%d\n", i);
408 atf_check -o file:exp "$(prog_db)" recno in
411 atf_test_case alternate_recno
412 alternate_recno_head()
414 atf_set "descr" "Checks recno database using alternating order entries"
416 alternate_recno_body()
418 TMPDIR="$(pwd)/db_dir"; export TMPDIR
423 for (i = 1; i < 1200; i += 2) {
425 s = substr($0, 1, i % 34);
428 printf("input key %d: %s\n", i, s);
430 for (i = 2; i < 1200; i += 2) {
432 s = substr($0, 1, i % 34);
435 printf("input key %d: %s\n", i, s);
446 printf("p\nk%d\nd%s\n", i, $0);
456 for (i = 1; i < 1200; ++i)
457 printf("g\nk%d\n", i);
460 atf_check "$(prog_db)" -o out recno in
465 cmp -s exp out || atf_fail "test failed"
470 TMPDIR="$(pwd)/db_dir"; export TMPDIR
477 for (i = 1; i <= 120; ++i)
478 printf("%05d: input key %d: %s\n", i, i, $0);
483 printf("p\nk%d\nd%s\n", ++i, $0);
487 for (i = 1; i <= 120; ++i)
489 printf("fR_CURSOR\ns\nkXX\n");
491 printf("fR_NEXT\ns\n");
492 printf("fR_CURSOR\ns\nk1\n");
494 printf("fR_FIRST\ns\n");
497 # For btree, the records are ordered by the string representation
498 # of the key value. So sort the expected output file accordingly,
499 # and set the seek_last key to the last expected key value.
501 if [ "$type" = "btree" ] ; then
502 sed -e 's/kXX/k99/' < in > tmp
504 sort -d -k4 < exp > tmp
508 printf("%05d: input key %d: %s\n", 99, 99, $0);
509 printf("seq failed, no such key\n");
510 printf("%05d: input key %d: %s\n", 1, 1, $0);
511 printf("%05d: input key %d: %s\n", 10, 10, $0);
515 # For recno, records are ordered by numerical key value. No sort
516 # is needed, but still need to set proper seek_last key value.
517 sed -e 's/kXX/k120/' < in > tmp
521 printf("%05d: input key %d: %s\n", 120, 120, $0);
522 printf("seq failed, no such key\n");
523 printf("%05d: input key %d: %s\n", 1, 1, $0);
524 printf("%05d: input key %d: %s\n", 2, 2, $0);
529 atf_check "$(prog_db)" -o out $type in
530 atf_check -o file:exp cat out
533 atf_test_case delete_btree
536 atf_set "descr" "Checks removing records in btree database"
543 atf_test_case delete_recno
546 atf_set "descr" "Checks removing records in recno database"
555 TMPDIR="$(pwd)/db_dir"; export TMPDIR
560 for (i = 1; i <= 10; ++i) {
561 printf("p\nkkey1\nD/bin/sh\n");
562 printf("p\nkkey2\nD/bin/csh\n");
564 printf("c\nkkey2\nD/bin/csh\n");
565 printf("c\nkkey1\nD/bin/sh\n");
566 printf("e\t%d of 10 (comparison)\n", i);
568 printf("e\t%d of 10 \n", i);
569 printf("r\nkkey1\nr\nkkey2\n");
576 atf_test_case repeated_btree
577 repeated_btree_head()
580 "Checks btree database with repeated small keys and" \
581 "big data pairs. Makes sure that overflow pages are reused"
583 repeated_btree_body()
588 atf_test_case repeated_hash
592 "Checks hash database with repeated small keys and" \
593 "big data pairs. Makes sure that overflow pages are reused"
600 atf_test_case duplicate_btree
601 duplicate_btree_head()
603 atf_set "descr" "Checks btree database with duplicate keys"
605 duplicate_btree_body()
607 TMPDIR="$(pwd)/db_dir"; export TMPDIR
612 for (i = 1; i <= 543; ++i)
613 printf("%05d: input key %d: %s\n", i, i, $0);
620 printf("p\nkduplicatekey\nd%s\n", $0);
622 printf("p\nkunique%dkey\nd%s\n", i, $0);
628 atf_check -o file:exp -x "$(prog_db) -iflags=1 btree in | sort"
633 TMPDIR="$(pwd)/db_dir"; export TMPDIR
640 for (i = 1; i <= 20; ++i)
641 printf("%05d: input key %d: %s\n", i, i, $0);
645 # Test that R_CURSOR doesn't succeed before cursor initialized
650 printf("p\nk%d\nd%s\n", ++i, $0);
653 printf("fR_CURSOR\nr\n");
654 printf("eR_CURSOR SHOULD HAVE FAILED\n");
657 atf_check -o ignore -e ignore -s ne:0 "$(prog_db)" -o out $type in
658 atf_check -s ne:0 test -s out
664 printf("p\nk%d\nd%s\n", ++i, $0);
667 printf("fR_CURSOR\np\nk1\ndsome data\n");
668 printf("eR_CURSOR SHOULD HAVE FAILED\n");
671 atf_check -o ignore -e ignore -s ne:0 "$(prog_db)" -o out $type in
672 atf_check -s ne:0 test -s out
675 atf_test_case cursor_flags_btree
676 cursor_flags_btree_head()
679 "Checks use of cursor flags without initialization in btree database"
681 cursor_flags_btree_body()
686 atf_test_case cursor_flags_recno
687 cursor_flags_recno_head()
690 "Checks use of cursor flags without initialization in recno database"
692 cursor_flags_recno_body()
697 atf_test_case reverse_order_recno
698 reverse_order_recno_head()
700 atf_set "descr" "Checks reverse order inserts in recno database"
702 reverse_order_recno_body()
704 TMPDIR="$(pwd)/db_dir"; export TMPDIR
709 for (i = 1; i <= 779; ++i)
710 printf("%05d: input key %d: %s\n", i, i, $0);
718 printf("p\nk1\nd%s\n", $0);
719 printf("%s\n", "fR_IBEFORE");
721 printf("p\nk1\nd%s\n", $0);
727 atf_check -o file:exp "$(prog_db)" recno in
730 atf_test_case small_page_btree
731 small_page_btree_head()
734 "Checks btree database with lots of keys and small page" \
735 "size: takes the first 20000 entries in the dictionary," \
736 "reverses them, and gives them each a small size data" \
737 "entry. Uses a small page size to make sure the btree" \
738 "split code gets hammered."
740 small_page_btree_body()
742 TMPDIR="$(pwd)/db_dir"; export TMPDIR
745 mdata=abcdefghijklmnopqrstuvwxy
747 awk '{ for (i = 1; i < 20001; ++i) print $0 }' >exp
749 for i in `sed 20000q $(dict) | rev`; do
757 atf_check -o file:exp "$(prog_db)" -i psize=512 btree in
762 TMPDIR="$(pwd)/db_dir"; export TMPDIR
768 for order in 1234 4321; do
769 for i in `sed 50q $(dict)`; do
777 atf_check -o file:exp "$(prog_db)" -ilorder=$order -f byte.file $type in
779 for i in `sed 50q $(dict)`; do
784 atf_check -o file:exp "$(prog_db)" -s -ilorder=$order -f byte.file $type in
788 atf_test_case byte_orders_btree
789 byte_orders_btree_head()
791 atf_set "descr" "Checks btree database using differing byte orders"
793 byte_orders_btree_body()
798 atf_test_case byte_orders_hash
799 byte_orders_hash_head()
801 atf_set "descr" "Checks hash database using differing byte orders"
803 byte_orders_hash_body()
813 echo "bucketsize $bsize, fill factor $ffactor"
814 atf_check -o file:exp "$(prog_db)" "-ibsize=$bsize,\
815 ffactor=$ffactor,nelem=25000,cachesize=65536" hash in
818 atf_test_case bsize_ffactor
821 atf_set "timeout" "1800"
822 atf_set "descr" "Checks hash database with various" \
823 "bucketsizes and fill factors"
827 TMPDIR="$(pwd)/db_dir"; export TMPDIR
832 for (i = 1; i <= 10000; ++i) {
834 s = substr($0, 1, i % 34);
849 s = substr(ds, 1, i % 34);
852 printf("p\nk%s\nd%s\n", $0, s);
858 printf("g\nk%s\n", $0);
861 h_bsize_ffactor 256 11
862 h_bsize_ffactor 256 14
863 h_bsize_ffactor 256 21
865 h_bsize_ffactor 512 21
866 h_bsize_ffactor 512 28
867 h_bsize_ffactor 512 43
869 h_bsize_ffactor 1024 43
870 h_bsize_ffactor 1024 57
871 h_bsize_ffactor 1024 85
873 h_bsize_ffactor 2048 85
874 h_bsize_ffactor 2048 114
875 h_bsize_ffactor 2048 171
877 h_bsize_ffactor 4096 171
878 h_bsize_ffactor 4096 228
879 h_bsize_ffactor 4096 341
881 h_bsize_ffactor 8192 341
882 h_bsize_ffactor 8192 455
883 h_bsize_ffactor 8192 683
885 h_bsize_ffactor 16384 341
886 h_bsize_ffactor 16384 455
887 h_bsize_ffactor 16384 683
889 h_bsize_ffactor 32768 341
890 h_bsize_ffactor 32768 455
891 h_bsize_ffactor 32768 683
896 h_bsize_ffactor 65536 341
897 h_bsize_ffactor 65536 455
898 h_bsize_ffactor 65536 683
904 # This tests 64K block size addition/removal
905 atf_test_case four_char_hash
906 four_char_hash_head()
909 "Checks hash database with 4 char key and" \
910 "value insert on a 65536 bucket size"
912 four_char_hash_body()
914 TMPDIR="$(pwd)/db_dir"; export TMPDIR
927 atf_check "$(prog_db)" -i bsize=32768 hash in
930 atf_check "$(prog_db)" -i bsize=65536 hash in
937 atf_test_case bsize_torture
940 atf_set "timeout" "36000"
941 atf_set "descr" "Checks hash database with various bucket sizes"
945 TMPDIR="$(pwd)/db_dir"; export TMPDIR
949 # db(3) doesn't support 64kB bucket sizes
950 for i in 2048 4096 8192 16384 32768 # 65536
953 atf_check "$(prog_lfsr)" $i
957 atf_init_test_cases()
959 atf_add_test_case small_btree
960 atf_add_test_case small_hash
961 atf_add_test_case small_recno
962 atf_add_test_case medium_btree
963 atf_add_test_case medium_hash
964 atf_add_test_case medium_recno
965 atf_add_test_case big_btree
966 atf_add_test_case big_hash
967 atf_add_test_case big_recno
968 atf_add_test_case random_recno
969 atf_add_test_case reverse_recno
970 atf_add_test_case alternate_recno
971 atf_add_test_case delete_btree
972 atf_add_test_case delete_recno
973 atf_add_test_case repeated_btree
974 atf_add_test_case repeated_hash
975 atf_add_test_case duplicate_btree
976 atf_add_test_case cursor_flags_btree
977 atf_add_test_case cursor_flags_recno
978 atf_add_test_case reverse_order_recno
979 atf_add_test_case small_page_btree
980 atf_add_test_case byte_orders_btree
981 atf_add_test_case byte_orders_hash
982 atf_add_test_case bsize_ffactor
983 atf_add_test_case four_char_hash
984 atf_add_test_case bsize_torture