[m-rev.] diff: fix off-by-one bug in version_hash_table.fold
Peter Wang
novalazy at gmail.com
Fri Jul 11 16:01:58 AEST 2008
Branches: main
library/version_hash_table.m:
Fix an off-by-one bug in version_hash_table.fold.
tests/hard_coded/Mmakefile:
tests/hard_coded/version_hash_table_test.exp:
tests/hard_coded/version_hash_table_test.m:
Add test case.
diff --git a/library/version_hash_table.m b/library/version_hash_table.m
index a4419fd..eca341f 100644
--- a/library/version_hash_table.m
+++ b/library/version_hash_table.m
@@ -507,7 +507,8 @@ munge(N, X, Y) =
%-----------------------------------------------------------------------------%
-fold(Fn, HT, X) = fold_up(apply_k_v(Fn, HT ^ buckets), 0, HT ^ num_buckets, X).
+fold(Fn, HT, X) =
+ fold_up(apply_k_v(Fn, HT ^ buckets), 0, HT ^ num_buckets - 1, X).
:- func apply_k_v(func(K, V, T) = T, buckets(K, V), int, T) = T.
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 62417bf..03f941b 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -528,6 +528,7 @@ ifeq "$(findstring profdeep,$(GRADE))" ""
tl_backjump_test \
user_defined_equality \
version_array_test \
+ version_hash_table_test \
write_binary
else
NON_PROFDEEP_PROGS =
diff --git a/tests/hard_coded/version_hash_table_test.exp b/tests/hard_coded/version_hash_table_test.exp
new file mode 100644
index 0000000..2e29d97
--- /dev/null
+++ b/tests/hard_coded/version_hash_table_test.exp
@@ -0,0 +1 @@
+["one" - 1, "three" - 3, "two" - 2]
diff --git a/tests/hard_coded/version_hash_table_test.m b/tests/hard_coded/version_hash_table_test.m
new file mode 100644
index 0000000..eefd049
--- /dev/null
+++ b/tests/hard_coded/version_hash_table_test.m
@@ -0,0 +1,42 @@
+%-----------------------------------------------------------------------------%
+% version_hash_table_test.m
+% vim: ft=mercury ts=4 sw=4 et
+%-----------------------------------------------------------------------------%
+
+:- module version_hash_table_test.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module list.
+:- import_module pair.
+:- import_module version_hash_table.
+
+%-----------------------------------------------------------------------------%
+
+main(!IO) :-
+ % Test `fold' which had an off-by-one bug.
+ some [!HT] (
+ !:HT = version_hash_table.new_default(generic_double_hash),
+ version_hash_table.set("one", 1, !HT),
+ version_hash_table.set("two", 2, !HT),
+ version_hash_table.set("three", 3, !HT),
+ version_hash_table.fold(concat, !.HT, []) = KVs,
+ list.sort(KVs, SortedKVs),
+ io.write(SortedKVs, !IO),
+ io.nl(!IO)
+ ).
+
+:- func concat(K, V, list(pair(K, V))) = list(pair(K, V)).
+
+concat(K, V, Acc) = [K - V | Acc].
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list