xref: /6.6.0/geocouch/vtree/test/007-io.t (revision 92def13f)
1#!/usr/bin/env escript
2%% -*- erlang -*-
3
4% Licensed under the Apache License, Version 2.0 (the "License"); you may not
5% use this file except in compliance with the License. You may obtain a copy of
6% the License at
7%
8%   http://www.apache.org/licenses/LICENSE-2.0
9%
10% Unless required by applicable law or agreed to in writing, software
11% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13% License for the specific language governing permissions and limitations under
14% the License.
15
16-include_lib("../include/vtree.hrl").
17
18-define(MOD, vtree_io).
19-define(FILENAME, "/tmp/vtree_io_vtree.bin").
20
21main(_) ->
22    % Set the random seed once, for the whole test suite
23    rand:seed(exrop, {1, 11, 91}),
24
25    % Apache CouchDB doesn't have the couch_file_write_guard module
26    try
27        couch_file_write_guard:sup_start_link()
28    catch error:undef ->
29        ok
30    end,
31
32    code:add_pathz(filename:dirname(escript:script_name())),
33    etap:plan(13),
34    case (catch test()) of
35        ok ->
36            etap:end_tests();
37        Other ->
38            % Somehow etap:diag/1 and etap:bail/1 don't work properly
39            %etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
40            %etap:bail(Other),
41            io:format(standard_error, "Test died abnormally:~n~p~n", [Other])
42     end.
43
44
45test() ->
46    test_encode_decode_kvnode_value(),
47    test_encode_decode_kpnode_value(),
48    test_encode_decode_kvnodes(),
49    test_encode_decode_kpnodes(),
50    test_write_read_nodes(),
51    test_encode_decode_mbb(),
52    ok.
53
54
55test_encode_decode_kvnode_value() ->
56    Fd = vtree_test_util:create_file(?FILENAME),
57    [Node1, Node2] = vtree_test_util:generate_kvnodes(2),
58
59    {Encoded1, _Size1} = ?MOD:encode_value(Node1),
60    % We flush as late as possible, hence for testing it needs to be done
61    % manually
62    geocouch_file:flush(Fd),
63    Decoded1 = ?MOD:decode_kvnode_value(Encoded1),
64    etap:is(Decoded1, Node1#kv_node{key=[], docid=nil, size=0},
65            "KV-node value got correctly encoded and decoded (a)"),
66
67    {Encoded2, _Size2} = ?MOD:encode_value(Node2),
68    geocouch_file:flush(Fd),
69    Decoded2 = ?MOD:decode_kvnode_value(Encoded2),
70    etap:is(Decoded2, Node2#kv_node{key=[], docid=nil, size=0},
71            "KV-node value got correctly encoded and decoded (b)"),
72    couch_file:close(Fd).
73
74
75test_encode_decode_kpnode_value() ->
76    Fd = vtree_test_util:create_file(?FILENAME),
77    [Node1, Node2] = vtree_test_util:generate_kpnodes(2),
78    {Encoded1, _Size1} = ?MOD:encode_value(Node1),
79    % We flush as late as possible, hence for testing it needs to be done
80    % manually
81    geocouch_file:flush(Fd),
82    Decoded1 = ?MOD:decode_kpnode_value(Encoded1),
83    etap:is(Decoded1, Node1#kp_node{key=[]},
84            "KP-node value got correctly encoded and decoded (a)"),
85
86    {Encoded2, _Size2} = ?MOD:encode_value(Node2),
87    geocouch_file:flush(Fd),
88    Decoded2 = ?MOD:decode_kpnode_value(Encoded2),
89    etap:is(Decoded2, Node2#kp_node{key=[]},
90            "KP-node value got correctly encoded and decoded (b)"),
91    couch_file:close(Fd).
92
93
94test_encode_decode_kvnodes() ->
95    Fd = vtree_test_util:create_file(?FILENAME),
96
97    Nodes1 = vtree_test_util:generate_kvnodes(1),
98    {Encoded1, _Size1} = ?MOD:encode_node(Nodes1),
99    % We flush as late as possible, hence for testing it needs to be done
100    % manually
101    geocouch_file:flush(Fd),
102    Decoded1 = ?MOD:decode_node(Encoded1),
103    etap:is(Decoded1, [N#kv_node{size=0} || N <- Nodes1],
104            "KV-node got correctly encoded and decoded (a)"),
105
106    Nodes2 = vtree_test_util:generate_kvnodes(5),
107    {Encoded2, _Size2} = ?MOD:encode_node(Nodes2),
108    geocouch_file:flush(Fd),
109    Decoded2 = ?MOD:decode_node(Encoded2),
110    etap:is(Decoded2, [N#kv_node{size=0} || N <- Nodes2],
111            "KV-node got correctly encoded and decoded (b)"),
112    couch_file:close(Fd).
113
114
115test_encode_decode_kpnodes() ->
116    Fd = vtree_test_util:create_file(?FILENAME),
117    Nodes1 = vtree_test_util:generate_kpnodes(1),
118    Nodes2 = vtree_test_util:generate_kpnodes(7),
119
120    {Encoded1, _Size1} = ?MOD:encode_node(Nodes1),
121    % We flush as late as possible, hence for testing it needs to be done
122    % manually
123    geocouch_file:flush(Fd),
124    Decoded1 = ?MOD:decode_node(Encoded1),
125    etap:is(Decoded1, Nodes1,
126            "KP-node got correctly encoded and decoded (a)"),
127
128    {Encoded2, _Size2} = ?MOD:encode_node(Nodes2),
129    geocouch_file:flush(Fd),
130    Decoded2 = ?MOD:decode_node(Encoded2),
131    etap:is(Decoded2, Nodes2,
132            "KP-node got correctly encoded and decoded (b)"),
133    couch_file:close(Fd).
134
135
136test_write_read_nodes() ->
137    Fd = vtree_test_util:create_file(?FILENAME),
138    Nodes1 = vtree_test_util:generate_kvnodes(6),
139    Nodes2 = vtree_test_util:generate_kpnodes(2),
140    Less = fun(A, B) -> A < B end,
141
142    {ok, ParentNode1} = ?MOD:write_node(Fd, Nodes1, Less),
143    NodesWritten1 = ?MOD:read_node(Fd, ParentNode1#kp_node.childpointer),
144    etap:is(NodesWritten1, Nodes1,
145            "KV-nodes were correctly written and read back"),
146
147    {ok, ParentNode2} = ?MOD:write_node(Fd, Nodes2, Less),
148    NodesWritten2 = ?MOD:read_node(Fd, ParentNode2#kp_node.childpointer),
149    etap:is(NodesWritten2, Nodes2,
150            "KP-nodes were correctly written and read back"),
151
152    couch_file:close(Fd).
153
154
155test_encode_decode_mbb() ->
156    Mbb1 = [{39.93, 48.9483}, {20, 90}, {-29.4, 83}],
157    Mbb2 = [{8,9}],
158    Mbb3 = [{-39.42, -4.2}, {48, 48}, {0, 3}],
159
160    etap:is(?MOD:decode_mbb(?MOD:encode_mbb(Mbb1)),
161            Mbb1,
162            "MBB got correctly encoded (a)"),
163    etap:is(?MOD:decode_mbb(?MOD:encode_mbb(Mbb2)),
164            Mbb2,
165            "MBB got correctly encoded (b)"),
166    etap:is(?MOD:decode_mbb(?MOD:encode_mbb(Mbb3)),
167            Mbb3,
168            "MBB got correctly encoded (c)").
169