summaryrefslogtreecommitdiff
path: root/test/etap/060-kt-merging.t
blob: 73744e5289fc470c2f81dabb94ad46cba53b6da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env escript
%% -*- erlang -*-

% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

main(_) ->
    test_util:init_code_path(),
    etap:plan(14),
    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail(Other)
    end,
    ok.

test() ->
    EmptyTree = [],
    One = [{0, {"1","foo",[]}}],
    TwoSibs = [{0, {"1","foo",[]}},
               {0, {"2","foo",[]}}],
    OneChild = [{0, {"1","foo",[{"1a", "bar", []}]}}],
    TwoChild = [{0, {"1","foo", [{"1a", "bar", [{"1aa", "bar", []}]}]}}],
    TwoChildSibs = [{0, {"1","foo", [{"1a", "bar", []},
                                     {"1b", "bar", []}]}}],
    TwoChildSibs2 = [{0, {"1","foo", [{"1a", "bar", []},
                                     {"1b", "bar", [{"1bb", "boo", []}]}]}}],
    Stemmed1b = [{1, {"1a", "bar", []}}],
    Stemmed1a = [{1, {"1a", "bar", [{"1aa", "bar", []}]}}],
    Stemmed1aa = [{2, {"1aa", "bar", []}}],
    Stemmed1bb = [{2, {"1bb", "boo", []}}],

    etap:is(
        {EmptyTree, no_conflicts},
        couch_key_tree:merge(EmptyTree, EmptyTree),
        "Merging two empty trees yields an empty tree."
    ),

    etap:is(
        {One, no_conflicts},
        couch_key_tree:merge(EmptyTree, One),
        "The empty tree is the identity for merge."
    ),

    etap:is(
        {One, no_conflicts},
        couch_key_tree:merge(One, EmptyTree),
        "Merging is commutative."
    ),

    etap:is(
        {TwoSibs, no_conflicts},
        couch_key_tree:merge(One, TwoSibs),
        "Merging a prefix of a tree with the tree yields the tree."
    ),

    etap:is(
        {One, no_conflicts},
        couch_key_tree:merge(One, One),
        "Merging is reflexive."
    ),

    etap:is(
        {TwoChild, no_conflicts},
        couch_key_tree:merge(TwoChild, TwoChild),
        "Merging two children is still reflexive."
    ),

    etap:is(
        {TwoChildSibs, no_conflicts},
        couch_key_tree:merge(TwoChildSibs, TwoChildSibs),
        "Merging a tree to itself is itself."),

    etap:is(
        {TwoChildSibs, no_conflicts},
        couch_key_tree:merge(TwoChildSibs, Stemmed1b),
        "Merging a tree with a stem."
    ),

    etap:is(
        {TwoChildSibs2, no_conflicts},
        couch_key_tree:merge(TwoChildSibs2, Stemmed1bb),
        "Merging a stem at a deeper level."
    ),

    etap:is(
        {TwoChild, no_conflicts},
        couch_key_tree:merge(TwoChild, Stemmed1aa),
        "Merging a single tree with a deeper stem."
    ),

    etap:is(
        {TwoChild, no_conflicts},
        couch_key_tree:merge(TwoChild, Stemmed1a),
        "Merging a larger stem."
    ),

    etap:is(
        {Stemmed1a, no_conflicts},
        couch_key_tree:merge(Stemmed1a, Stemmed1aa),
        "More merging."
    ),

    Expect1 = OneChild ++ Stemmed1aa,
    etap:is(
        {Expect1, conflicts},
        couch_key_tree:merge(OneChild, Stemmed1aa),
        "Merging should create conflicts."
    ),

    etap:is(
        {TwoChild, no_conflicts},
        couch_key_tree:merge(Expect1, TwoChild),
        "Merge should have no conflicts."
    ),

    ok.