Files
mercury/tests/hard_coded/tree_bitset_difference.m
Zoltan Somogyi 9cf01fa147 Fix two more problems with tree_bitset.difference.
Estimated hours taken: 5
Branches: main

Fix two more problems with tree_bitset.difference.

The first problem, fixed by Peter Wang, was a stray "+1". This was in code
that tried to figure out the difference between two lists of nodes that could
be at different levels, and occurred when the level of the first operand
was lower than the level of the second.

The second problem occurred when the level of the first operand was greater
than the level of the second. The code tried to handle this case, but did not
do so correctly, mainly because it assumed that raising by one level is always
enough.

The underlying cause of both problems was that the predicate involved
(interiorlist_difference) tried to do too much: compute the difference AND
match the levels of the input operands. The overall fix is to get the caller
to ensure that the two operands are at the same level.

This diff was tested with
- set_of_var using test_bitset instead of tree_bitset, to test the results
  of every set_of_var operation,
- with tree_bitset's bits_per_level set to 1, to force the execution of code
  dealing with different levels of nodes much more frequently than the usual
  bits_per_level=5 (with the latter setting, only enormous sets ever have more
  than one level of interior node), and
- with integrity tests on tree_bitset.m enabled.

library/tree_bitset.m:
	Make the above change.

tests/hard_coded/tree_bitset_difference.{m,exp}:
	Update the test case to test all the operand pairs listed in the Mantis
	bug report, in two forms: one that showed the bug on 32-bit systems,
	and one that showed the bug on 64-bit systems. (The word size matters
	because one leaf node contains one word's worth of bits.)

	Since we now generate much more output, checking the equivalence
	of the results generated by tree_bitset.m and by set.m by eye is no
	longer trivial. Make the code do the check itself, and print output
	only if there is a mismatch.
2011-08-04 02:01:39 +00:00

84 lines
2.7 KiB
Mathematica

%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sts=4 sw=4 et
%-----------------------------------------------------------------------------%
% This is a regression test for Mantis bug #207.
%-----------------------------------------------------------------------------%
:- module tree_bitset_difference.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module int.
:- import_module list.
:- import_module pair.
:- import_module set.
:- import_module tree_bitset.
%-----------------------------------------------------------------------------%
main(!IO) :-
Tests = [
% The values of X and Y (or 2X and 2Y) are intended to end up
% in near the start and the end of one interior node, while Z (or 2Z)
% ends up near the start of the next interior node. The bug was in how
% the difference operation handled interior nodes at the same level
% but not at the same starting address.
[532, 32431] - [32794],
[1, 29424] - [1, 2, 3, 35701],
[1] - [2, 35701],
[101, 102] - [1, 2, 3, 35699, 35700, 35701],
[36696, 35702, 35703, 35705] -
[1, 2, 3, 33416, 334283]
],
list.foldl(test_32_64, Tests, !IO).
:- pred test_32_64(pair(list(int), list(int))::in, io::di, io::uo) is det.
test_32_64(ListA - ListB, !IO) :-
test(ListA, ListB, !IO),
test(list.map(double, ListA), list.map(double, ListB), !IO).
:- func double(int) = int.
double(X) = 2 * X.
:- pred test(list(int)::in, list(int)::in, io::di, io::uo) is det.
test(ListA, ListB, !IO) :-
SetA = set.from_list(ListA),
SetB = set.from_list(ListB),
set.difference(SetA, SetB, SetC),
set.to_sorted_list(SetC, ListC_set),
BitSetA = tree_bitset.list_to_set(ListA),
BitSetB = tree_bitset.list_to_set(ListB),
tree_bitset.difference(BitSetA, BitSetB, BitSetC),
ListC_bitset = tree_bitset.to_sorted_list(BitSetC),
( ListC_set = ListC_bitset ->
true
;
io.write_string("DIFFERENCE:\n", !IO),
io.write_string("list A: ", !IO),
io.write(ListA, !IO),
io.nl(!IO),
io.write_string("list B: ", !IO),
io.write(ListB, !IO),
io.nl(!IO),
io.write_string("set difference: ", !IO),
io.write(ListC_set, !IO),
io.nl(!IO),
io.write_string("tree_bitset difference: ", !IO),
io.write(ListC_bitset, !IO),
io.nl(!IO)
).