aboutsummaryrefslogblamecommitdiffstats
path: root/lib/stdlib/test/file_sorter_SUITE.erl
blob: e0d9ec1fd70aa760c6bf8e8ddb39ab634256b48d (plain) (tree)
1
2
3
4
5
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359


                   
                                                        
   










                                                                           













                                                
                                                    



                                                

                                                                    










                                                     
                                                   




                                   
                                  



                                     
                                         
 
         





                                                            



            





                         
                                     
           

                                    
           
 







                                     
                                 












































































































































































































































































































































































                                                                               
                                 

































































                                                                                
                                 































































                                                                               
                                 



















































































































                                                                              
                                 







































































                                                                              
                                 

























































                                                                               
                                 



































                                                                       
                                 

































































































































































































































































































































































































































































































                                                                                
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2011. All Rights Reserved.
%% 
%% 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.
%% 
%% %CopyrightEnd%
%%
-module(file_sorter_SUITE).

%-define(debug, true).

-ifdef(debug).
-define(format(S, A), io:format(S, A)).
-define(line, put(line, ?LINE), ).
-define(config(X,Y), foo).
-define(t,test_server).
-define(privdir(_), "./file_sorter_SUITE_priv").
-else.
-include_lib("test_server/include/test_server.hrl").
-define(format(S, A), ok).
-define(privdir(Conf), ?config(priv_dir, Conf)).
-endif.

-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
	 init_per_group/2,end_per_group/2, basic/1, badarg/1, 
	 term_sort/1, term_keysort/1,
	 binary_term_sort/1, binary_term_keysort/1,
	 binary_sort/1, 
	 term_merge/1, term_keymerge/1, 
	 binary_term_merge/1, binary_term_keymerge/1,
	 binary_merge/1,
	 term_check/1, term_keycheck/1,
	 binary_term_check/1, binary_term_keycheck/1,
	 binary_check/1,
	 inout/1, misc/1, many/1]).

-export([init_per_testcase/2, end_per_testcase/2]).

init_per_testcase(_Case, Config) ->
    Dog=?t:timetrap(?t:minutes(2)),
    [{watchdog, Dog}|Config].

end_per_testcase(_Case, Config) ->
    Dog=?config(watchdog, Config),
    test_server:timetrap_cancel(Dog),
    ok.

suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    [basic, badarg, term_sort, term_keysort,
     binary_term_sort, binary_term_keysort, binary_sort,
     term_merge, term_keymerge, binary_term_merge,
     binary_term_keymerge, binary_merge, term_check,
     binary_term_keycheck, binary_term_check,
     binary_term_keycheck, binary_check, inout, misc, many].

groups() -> 
    [].

init_per_suite(Config) ->
    Config.

end_per_suite(_Config) ->
    ok.

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


basic(doc) ->
    ["Basic test case."];
basic(suite) -> 
    [];
basic(Config) when is_list(Config) ->
    Fmt = binary,
    Arg = {format,Fmt},
    Foo = outfile("foo", Config),
    P0 = pps(),

    ?line F1s = [F1] = to_files([[]], Fmt, Config),
    ?line ok = file_sorter:sort(F1),
    ?line [] = from_files(F1, Fmt),
    ?line ok = file_sorter:keysort(17, F1),
    ?line [] = from_files(F1, Fmt),
    ?line ok = file_sorter:merge(F1s, Foo),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keymerge(17, F1s, Foo),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files([Foo | F1s]),

    ?line [F2] = to_files([[foo,bar]], Fmt, Config),
    ?line ok = file_sorter:sort([F2], F2, Arg),
    ?line [bar,foo] = from_files(F2, Fmt),
    ?line delete_files(F2),

    ?line Fs1 = to_files([[foo],[bar]], Fmt, Config),
    ?line ok = file_sorter:sort(Fs1, Foo, Arg),
    ?line [bar,foo] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:merge(Fs1, Foo, Arg),
    ?line [bar,foo] = from_files(Foo, Fmt),
    ?line delete_files([Foo | Fs1]),

    ?line Fmt2 = binary_term,
    ?line Arg2 = {format, Fmt2},
    ?line [F3] = to_files([[{foo,1},{bar,2}]], Fmt2, Config),
    ?line ok = file_sorter:keysort([2], [F3], F3, Arg2),
    ?line [{foo,1},{bar,2}] = from_files(F3, Fmt2),
    ?line delete_files(F3),

    ?line Fs2 = to_files([[{foo,1}],[{bar,2}]], Fmt2, Config),
    ?line ok = file_sorter:keysort(1, Fs2, Foo, Arg2),
    ?line [{bar,2},{foo,1}] = from_files(Foo, Fmt2),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keymerge(1, Fs2, Foo, Arg2),
    ?line [{bar,2},{foo,1}] = from_files(Foo, Fmt2),
    ?line delete_files([Foo | Fs2]),

    ?line true = P0 =:= pps(),

    ok.

badarg(doc) ->
    ["Call functions with bad arguments."];
badarg(suite) -> 
    [];
badarg(Config) when is_list(Config) ->
    PrivDir = ?privdir(Config),
    BadFile = filename:join(PrivDir, "not_a_file"),
    ABadFile = filename:absname(BadFile),
    ?line file:delete(BadFile),
    ?line {error,{file_error,ABadFile,enoent}} = 
	file_sorter:sort(BadFile),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:sort({flipp})),
    ?line {error,{file_error,ABadFile,enoent}} = 
	file_sorter:keysort(1, BadFile),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:keysort(1, {flipp})),

    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:merge([{flipp}],foo)),
    ?line {error,{file_error,ABadFile,enoent}} = 
	file_sorter:keymerge(1,[BadFile],foo),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:keymerge(1,[{flipp}],foo)),
    ?line {'EXIT', {{badarg, _}, _}} = 
	(catch file_sorter:merge(fun(X) -> X end, foo)),
    ?line {'EXIT', {{badarg, _}, _}} = 
	(catch file_sorter:keymerge(1, fun(X) -> X end, foo)),

    ?line {error,{file_error,ABadFile,enoent}} = 
	file_sorter:check(BadFile),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:check({flipp})),
    ?line {error,{file_error,ABadFile,enoent}} = 
	file_sorter:keycheck(1, BadFile),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:keycheck(1, {flipp})),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:check([{flipp}],foo)),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:keycheck(1,[{flipp}],foo)),
    ?line {'EXIT', {{badarg, _}, _}} = 
	(catch file_sorter:check(fun(X) -> X end, foo)),
    ?line {'EXIT', {{badarg, _}, _}} = 
	(catch file_sorter:keycheck(1, fun(X) -> X end, foo)),

    ?line Fs1 = to_files([[1,2,3]], binary_term, Config),
    ?line {'EXIT', {{badarg, flipp}, _}} = 
	(catch file_sorter:check(Fs1 ++ flipp, [])),
    [F1] = Fs1,
    ?line {error,{file_error,_,_}} = 
        file_sorter:sort(Fs1, foo, [{tmpdir,F1},{size,0}]),
    ?line delete_files(Fs1),
    ?line Fs2 = to_files([[1,2,3]], binary_term, Config),
    {error,{file_error,_,enoent}} = 
	file_sorter:sort(Fs2, foo, [{tmpdir,filename:absname(BadFile)},
                                    {size,0}]),
    ?line delete_files(Fs2),

    ?line {'EXIT', {{badarg, bad}, _}} = 
	(catch file_sorter:check([], [{format,term} | bad])),
    ?line {'EXIT', {{badarg, [{flipp}]}, _}} = 
	(catch file_sorter:check([{flipp}])),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch file_sorter:keycheck(1, {flipp})),
    ?line {'EXIT', {{badarg, [{flipp}]}, _}} = 
	(catch file_sorter:keycheck(2, [{flipp}])),
    ?line {error,{file_error,_,eisdir}} = file_sorter:keycheck(1, []),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch file_sorter:keycheck(kp, [])),
    ?line {'EXIT', {{badarg, kp}, _}} = 
	(catch file_sorter:keycheck([1, kp], [])),
    ?line {'EXIT', {{badarg, kp}, _}} = 
	(catch file_sorter:keycheck([1 | kp], [])),
    ?line {'EXIT', {{badarg, []}, _}} = (catch file_sorter:keycheck([], [])),
    ?line {'EXIT', {{badarg, {format, foo}}, _}} = 
	(catch file_sorter:check([], {format,foo})),
    ?line {'EXIT', {{badarg, not_an_option}, _}} = 
	(catch file_sorter:keycheck(7, [], [not_an_option])),
    ?line {'EXIT', {{badarg, format}, _}} = 
	(catch file_sorter:keycheck(1, [], [{format, binary}])),
    ?line {'EXIT', {{badarg, order}, _}} = 
	(catch file_sorter:keycheck(1, [], [{order, fun compare/2}])),

    ?line do_badarg(fun(I, O) -> file_sorter:sort(I, O) end,
		    fun(Kp, I, O) -> file_sorter:keysort(Kp, I, O) end,
		    BadFile),
    ?line do_badarg_opt(fun(I, O, X) -> file_sorter:sort(I, O, X) end,
			fun(Kp, I, O, X) -> file_sorter:keysort(Kp, I, O, X) 
			end),
    ?line do_badarg(fun(I, O) -> file_sorter:merge(I, O) end,
		    fun(Kp, I, O) -> file_sorter:keymerge(Kp, I, O) end, 
		    BadFile),
    ?line do_badarg_opt(fun(I, O, X) -> file_sorter:merge(I, O, X) end,
			fun(Kp, I, O, X) -> file_sorter:keymerge(Kp, I, O, X) 
			end).

do_badarg(F, KF, BadFile) ->
    [Char | _] = BadFile,
    AFlipp = filename:absname(flipp),
    ?line {error,{file_error,AFlipp,enoent}} = F([flipp | flopp], foo),
    ?line {'EXIT', {{badarg, {foo,bar}}, _}} = (catch F([], {foo,bar})),
    ?line {'EXIT', {{badarg, Char}, _}} = (catch F(BadFile, [])),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = (catch F({flipp}, [])),

    ?line {'EXIT', {{badarg, Char}, _}} = (catch KF(1, BadFile, [])),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = (catch KF(1, {flipp}, [])),
    ?line {error,{file_error,AFlipp,enoent}} = 
	KF(2, [flipp | flopp], foo),
    ?line {'EXIT', {{badarg, {foo,bar}}, _}} = (catch KF(1, [], {foo,bar})),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch KF(kp, [], foo)),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch KF([1, kp], [], foo)),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch KF([1 | kp], [], foo)),
    ?line {'EXIT', {{badarg, []}, _}} = (catch KF([], [], foo)),
    ok.

do_badarg_opt(F, KF) ->
    AFlipp = filename:absname(flipp),
    ?line {error,{file_error,AFlipp,enoent}} = 
	   F([flipp | flopp], foo, []),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = (catch F([{flipp}], foo, [])),
    ?line {'EXIT', {{badarg, {out,put}}, _}} = (catch F([], {out,put}, [])),
    ?line {'EXIT', {{badarg, not_an_option}, _}} = 
	(catch F([], foo, [not_an_option])),
    ?line {'EXIT', {{badarg, {format, foo}}, _}} = 
	   (catch F([], foo, {format,foo})),
    ?line {'EXIT', {{badarg, {size,foo}}, _}} = (catch F([], foo, {size,foo})),

    ?line {'EXIT', {{badarg, {size, -1}}, _}} = (catch F([], foo, {size,-1})),
    ?line {'EXIT', {{badarg, {no_files, foo}}, _}} = 
	(catch F([], foo, {no_files,foo})),
    ?line {'EXIT', {{badarg, {no_files, 1}}, _}} = 
	(catch F([], foo, {no_files,1})),
    ?line {'EXIT', {{badarg, 1}, _}} = (catch F([], foo, {tmpdir,1})),
    ?line {'EXIT', {{badarg, {order,1}}, _}} = (catch F([], foo, {order,1})),
    ?line {'EXIT', {{badarg, {compressed, flopp}}, _}} = 
	   (catch F([], foo, {compressed,flopp})),
    ?line {'EXIT', {{badarg, {unique,flopp}}, _}} = 
	   (catch F([], foo, {unique,flopp})),
    ?line {'EXIT', {{badarg, {header,foo}}, _}} = 
	(catch F([], foo, {header,foo})),
    ?line {'EXIT', {{badarg, {header, 0}}, _}} = 
	(catch F([], foo, {header,0})),
    ?line {'EXIT', {{badarg, {header, 1 bsl 35}}, _}} = 
	(catch F([], foo, {header,1 bsl 35})),
    ?line {'EXIT', {{badarg, header}, _}} = 
	(catch F([], foo, [{header,1},{format,term}])),

    ?line {'EXIT', {{badarg, not_an_option}, _}} = 
	(catch KF(7, [], foo, [not_an_option])),
    ?line {'EXIT', {{badarg,format}, _}} = 
	(catch KF(1, [], foo, [{format, binary}])),
    ?line {'EXIT', {{badarg, order}, _}} = 
	(catch KF(1, [], foo, [{order, fun compare/2}])),
    ?line {'EXIT', {{badarg, {flipp}}, _}} = 
	(catch KF(2, [{flipp}], foo,[])),
    ?line {error,{file_error,AFlipp,enoent}} = 
	KF(2, [flipp | flopp], foo,[]),
    ?line {'EXIT', {{badarg, {out, put}}, _}} = 
	(catch KF(1, [], {out,put}, [])),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch KF(kp, [], foo, [])),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch KF([1, kp], [], foo, [])),
    ?line {'EXIT', {{badarg, kp}, _}} = (catch KF([1 | kp], [], foo, [])),
    ok.

term_sort(doc) ->
    ["Sort terms on files."];
term_sort(suite) ->
    [];
term_sort(Config) when is_list(Config) ->
    ?line sort(term, [{compressed,false}], Config),
    ?line sort(term, [{order, fun compare/2}], Config),
    ?line sort(term, [{order, ascending}, {compressed,true}], Config),
    ?line sort(term, [{order, descending}], Config),
    ok.

term_keysort(doc) ->
    ["Keysort terms on files."];
term_keysort(suite) ->
    [];
term_keysort(Config) when is_list(Config) ->
    ?line keysort(term, [{tmpdir, ""}], Config),
    ?line keysort(term, [{order,descending}], Config),
    ok.

binary_term_sort(doc) ->
    ["Sort binary terms on files."];
binary_term_sort(suite) ->
    [];
binary_term_sort(Config) when is_list(Config) ->
    PrivDir = ?privdir(Config),
    ?line sort({2, binary_term}, [], Config),
    ?line sort(binary_term, [{tmpdir, list_to_atom(PrivDir)}], Config),
    ?line sort(binary_term, [{tmpdir,PrivDir}], Config),
    ?line sort({3,binary_term}, [{order, fun compare/2}], Config),
    ?line sort(binary_term, [{order, fun compare/2}], Config),
    ?line sort(binary_term, [{order,descending}], Config),
    ok.

binary_term_keysort(doc) ->
    ["Keysort binary terms on files."];
binary_term_keysort(suite) ->
    [];
binary_term_keysort(Config) when is_list(Config) ->
    ?line keysort({3, binary_term}, [], Config),
    ?line keysort(binary_term, [], Config),
    ?line keysort(binary_term, [{order,descending}], Config),
    ok.

binary_sort(doc) ->
    ["Sort binaries on files."];
binary_sort(suite) ->
    [];
binary_sort(Config) when is_list(Config) ->
    PrivDir = ?privdir(Config),
    ?line sort({2, binary}, [], Config),
    ?line sort(binary, [{tmpdir, list_to_atom(PrivDir)}], Config),
    ?line sort(binary, [{tmpdir,PrivDir}], Config),
    ?line sort({3,binary}, [{order, fun compare/2}], Config),
    ?line sort(binary, [{order, fun compare/2}], Config),
    ?line sort(binary, [{order,descending}], Config),
    ok.

term_merge(doc) ->
    ["Merge terms on files."];
term_merge(suite) ->
    [];
term_merge(Config) when is_list(Config) ->
    ?line merge(term, [{order, fun compare/2}], Config),
    ?line merge(term, [{order, ascending}, {compressed,true}], Config),
    ?line merge(term, [{order, descending}, {compressed,false}], Config),
    ok.

term_keymerge(doc) ->
    ["Keymerge terms on files."];
term_keymerge(suite) ->
    [];
term_keymerge(Config) when is_list(Config) ->
    ?line keymerge(term, [], Config),
    ?line keymerge(term, [{order, descending}], Config),
    ?line funmerge(term, [], Config),
    ok.

binary_term_merge(doc) ->
    ["Merge binary terms on files."];
binary_term_merge(suite) ->
    [];
binary_term_merge(Config) when is_list(Config) ->
    ?line merge(binary_term, [], Config),
    ?line merge({7, binary_term}, [], Config),
    ?line merge({3, binary_term}, [{order, fun compare/2}], Config),
    ok.

binary_term_keymerge(doc) ->
    ["Keymerge binary terms on files."];
binary_term_keymerge(suite) ->
    [];
binary_term_keymerge(Config) when is_list(Config) ->
    ?line keymerge({3, binary_term}, [], Config),
    ?line keymerge(binary_term, [], Config),
    ?line funmerge({3, binary_term}, [], Config),
    ?line funmerge(binary_term, [], Config),
    ok.

binary_merge(doc) ->
    ["Merge binaries on files."];
binary_merge(suite) ->
    [];
binary_merge(Config) when is_list(Config) ->
    ?line merge(binary, [], Config),
    ?line merge({7, binary}, [], Config),
    ?line merge({3, binary}, [{order, fun compare/2}], Config),
    ok.

term_check(doc) ->
    ["Check terms on files."];
term_check(suite) ->
    [];
term_check(Config) when is_list(Config) ->
    ?line check(term, Config),
    ok.

binary_term_check(doc) ->
    ["Check binary terms on files."];
binary_term_check(suite) ->
    [];
binary_term_check(Config) when is_list(Config) ->
    ?line check(binary_term, Config),
    ok.

term_keycheck(doc) ->
    ["Keycheck terms on files."];
term_keycheck(suite) ->
    [];
term_keycheck(Config) when is_list(Config) ->
    ?line keycheck(term, Config),
    ok.

binary_term_keycheck(doc) ->
    ["Keycheck binary terms on files."];
binary_term_keycheck(suite) ->
    [];
binary_term_keycheck(Config) when is_list(Config) ->
    ?line keycheck(binary_term, Config),
    ok.

binary_check(doc) ->
    ["Check binary terms on files."];
binary_check(suite) ->
    [];
binary_check(Config) when is_list(Config) ->
    ?line check(binary, Config),
    ok.

inout(doc) ->
    ["Funs as input or output."];
inout(suite) ->
    [];
inout(Config) when is_list(Config) ->
    BTF = {format, binary_term},
    Foo = outfile("foo", Config),

    %% Input is fun.
    End = fun(read) -> end_of_input end,

    IF1 = fun(read) -> {[1,7,5], End} end,
    ?line ok = file_sorter:sort(IF1, Foo, [{format, term}]),
    %% 'close' is called, but the return value is caught and ignored.
    IF2 = fun(read) -> {[1,2,3], fun(close) -> throw(ignored) end} end,
    ?line {error, bad_object} = file_sorter:sort(IF2, Foo, BTF),

    IF3 = fun(no_match) -> foo end,
    ?line {'EXIT', {function_clause, _}} = 
	(catch file_sorter:sort(IF3, Foo)),
    IF4 = fun(read) -> throw(my_message) end,
    ?line my_message = (catch file_sorter:sort(IF4, Foo)),
    IF5 = fun(read) -> {error, my_error} end,
    ?line {error, my_error} = file_sorter:sort(IF5, Foo),

    %% Output is fun.
    ?line {error, bad_object} = 
	file_sorter:sort(IF2, fun(close) -> ignored end, BTF),
    Args = [{format, term}],
    ?line {error, bad_object} = 
       file_sorter:keysort(1, IF2, fun(close) -> ignored end, Args),
    OF1 = fun(close) -> fine; (L) when is_list(L) -> fun(close) -> nice end end,
    ?line nice = file_sorter:sort(IF1, OF1, Args),
    OF2 = fun(_) -> my_return end,
    ?line my_return = file_sorter:sort(IF1, OF2, Args),
    OF3 = fun(_) -> throw(my_message) end,
    ?line my_message = (catch file_sorter:sort(IF1, OF3, Args)),
    OF4 = fun(no_match) -> foo end,
    ?line {'EXIT', {function_clause, _}} = 
	(catch file_sorter:sort(IF1, OF4, Args)),

    ?line P0 = pps(),
    ?line Fs1 = to_files([[3,1,2,5,4], [8,3,10]], term, Config),
    ?line error = file_sorter:sort(Fs1, fun(_) -> error end, Args),
    ?line delete_files(Fs1),

    ?line true = P0 =:= pps(),

    %% Passing a value from the input functions to the output functions.
    IFV1 = fun(read) -> {end_of_input, 17} end,
    OFV1 = fun({value, Value}) -> ofv(Value, []) end,
    ?line {17, []} = file_sorter:sort(IFV1, OFV1, Args),

    %% Output is not a fun. The value returned by input funs is ignored.
    %% OTP-5009.
    ?line ok = file_sorter:sort(IFV1, Foo, [{format,term}]),
    ?line [] = from_files(Foo, term),
    ?line delete_files(Foo),

    ok.

ofv(Value, A) ->
    fun(close) -> 
	    {Value, lists:append(lists:reverse(A))};
       (L) when is_list(L) ->
	    ofv(Value, [L | A])
    end.

many(doc) ->
    ["Many temporary files."];
many(suite) ->
    [];
many(Config) when is_list(Config) ->
    Foo = outfile("foo", Config),
    PrivDir = ?privdir(Config),
    P0 = pps(),

    Args = [{format, term}],
    L1 = lists:map(fun(I) -> {one, two, three, I} end, lists:seq(1,1000)),
    L2 = lists:map(fun(I) -> {four, five, six, I} end, lists:seq(1,1000)),
    ?line Fs2 = to_files([L1, L2], term, Config),
    ?line ok = file_sorter:sort(Fs2, Foo, [{size,1000} | Args]),
    ?line R = lists:sort(L1++L2),
    ?line R = from_files(Foo, term),
    ?line 2000 = length(R),
    ?line ok = file_sorter:sort(Fs2, Foo, [{no_files,4},{size,1000} | Args]),
    ?line R = from_files(Foo, term),
    ?line ok = 
	file_sorter:sort(Fs2, Foo, 
			 [{no_files,4},{size,1000},{order,descending} | Args]),
    ?line true = lists:reverse(R) =:= from_files(Foo, term),
    ?line ok = 
	file_sorter:sort(Fs2, Foo, 
			 [{no_files,4},{size,1000},
			  {order,fun compare/2} | Args]),
    ?line R = from_files(Foo, term),
    ?line ok = file_sorter:keysort(4, Fs2, Foo, 
				   [{no_files,4},{size,1000} | Args]),
    ?line RK = lists:keysort(4, L1++L2),
    ?line RK = from_files(Foo, term),
    ?line delete_files(Foo),
    ?line ok = 
	file_sorter:keysort(4, Fs2, Foo, 
		      [{no_files,4},{size,1000},{order,descending} | Args]),
    ?line true = lists:reverse(RK) =:= from_files(Foo, term),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keysort(4, Fs2, Foo, 
				   [{size,500},{order,descending} | Args]),
    ?line true = lists:reverse(RK) =:= from_files(Foo, term),
    ?line delete_files(Foo),
    ?line error = file_sorter:sort(Fs2, fun(_) -> error end, 
				   [{tmpdir, PrivDir}, {no_files,3}, 
				    {size,10000} | Args]),

    TmpDir = filename:join(PrivDir, "tmpdir"),
    file:del_dir(TmpDir),
    ?line ok = file:make_dir(TmpDir),
    ?line case os:type() of
	      {unix, _} ->
		  ?line ok = file:change_mode(TmpDir, 8#0000),
		  ?line {error, {file_error, _,_}} = 
		      file_sorter:sort(Fs2, fun(_M) -> foo end, 
				       [{no_files,3},{size,10000},
					{tmpdir,TmpDir} | Args]);
	      _ ->
		  true
	  end,
    ?line ok = file:del_dir(TmpDir),
    delete_files(Fs2),
    ?line true = P0 =:= pps(),
    ok.

misc(doc) ->
    ["Some other tests."];
misc(suite) ->
    [];
misc(Config) when is_list(Config) ->
    BTF = {format, binary_term},
    Foo = outfile("foo", Config),
    FFoo = filename:absname(Foo),
    P0 = pps(),

    ?line [File] = Fs1 = to_files([[1,3,2]], term, Config),
    ?line ok = file:write_file(Foo,<<>>),
    ?line case os:type() of
	      {unix, _} ->
                  ok = file:change_mode(Foo, 8#0000),
                  {error,{file_error,FFoo,eacces}} = 
                      file_sorter:sort(Fs1, Foo, {format,term});
              _ ->
                  true
          end,
    ?line file:delete(Foo),
    ?line NoBytes = 16, % RAM memory will never get this big, or?
    ?line ALot = (1 bsl (NoBytes*8)) - 1,
    ?line ok = file:write_file(File, <<ALot:NoBytes/unit:8,"foobar">>),
    FFile = filename:absname(File),
    ?line {error, {bad_object,FFile}} = 
        file_sorter:sort(Fs1, Foo, [BTF, {header, 20}]),
    ?line ok = file:write_file(File, <<30:32,"foobar">>),
    ?line {error, {premature_eof, FFile}} = file_sorter:sort(Fs1, Foo, BTF),
    ?line ok = file:write_file(File, <<6:32,"foobar">>),
    ?line {error, {bad_object,FFile}} = file_sorter:sort(Fs1, Foo, BTF),
    ?line case os:type() of
              {unix, _} ->
                  ok = file:change_mode(File, 8#0000),
                  {error, {file_error,FFile,eacces}} = 
                      file_sorter:sort(Fs1, Foo),
                  {error, {file_error,FFile,eacces}} = 
                      file_sorter:sort(Fs1, Foo, {format, binary_term});
              _ ->
                  true
          end,
    ?line delete_files(Fs1),
    ?line true = P0 =:= pps(),

    %% bigger than chunksize
    ?line E1 = <<32000:32, 10:256000>>,
    ?line E2 = <<32000:32, 5:256000>>,
    ?line E3 = <<32000:32, 8:256000>>,
    ?line ok = file:write_file(Foo, [E1, E2, E3]),
    ?line ok = file_sorter:sort([Foo], Foo, [{format,binary},{size,10000}]),
    ?line ok = file_sorter:sort([Foo], Foo, [{format,fun(X) -> X end},
                                             {size,10000}]),
    ?line Es = list_to_binary([E2,E3,E1]),
    ?line {ok, Es} = file:read_file(Foo),
    ?line delete_files(Foo),
    ?line true = P0 =:= pps(),

    %% keysort more than one element
    L = [{c,1,a},{c,2,b},{c,3,c},{b,1,c},{b,2,b},{b,3,a},{a,1,a},{a,2,b},
         {a,3,c}],
    ?line Fs2 = to_files([L], binary_term, Config),
    ?line ok = file_sorter:keysort([2,3], Fs2, Foo, {format, binary_term}),
    ?line KS2_1 = from_files(Foo, binary_term),
    ?line KS2_2 = lists:keysort(2,lists:keysort(3, L)),
    ?line KS2_1 = KS2_2,
    ?line ok = file_sorter:keysort([2,3], Fs2, Foo, 
                                   [{format, binary_term},{size,5}]),
    ?line KS2_3 = from_files(Foo, binary_term),
    ?line KS2_3 = KS2_2,
    ?line ok = file_sorter:keysort([2,3,1], Fs2, Foo, {format, binary_term}),
    ?line KS3_1 = from_files(Foo, binary_term),
    ?line KS3_2 = lists:keysort(2, lists:keysort(3,lists:keysort(1, L))),
    ?line KS3_1 = KS3_2,
    ?line ok = file_sorter:keysort([2,3,1], Fs2, Foo, 
                                   [{format, binary_term},{size,5}]),
    ?line KS3_3 = from_files(Foo, binary_term),
    ?line KS3_3 = KS3_2,
    ?line delete_files([Foo | Fs2]),
    ?line true = P0 =:= pps(),

    %% bigger than chunksize
    %% Assumes that CHUNKSIZE = 16384. Illustrates that the Last argument
    %% of merge_files/5 is necessary. 
    ?line EP1 = erlang:make_tuple(2728,foo),
    ?line EP2 = lists:duplicate(2729,qqq),
    ?line LL = [EP1, EP2, EP1, EP2, EP1, EP2],
    ?line Fs3 = to_files([LL], binary, Config),
    ?line ok = file_sorter:sort(Fs3, Foo, [{format,binary}, {unique,true}]),
    ?line [EP1,EP2] = from_files(Foo, binary),
    ?line delete_files(Foo),
    ?line ok = file_sorter:sort(Fs3, Foo, 
                                [{format,binary_term}, {unique,true}, 
                                 {size,30000}]),
    ?line [EP1,EP2] = from_files(Foo, binary_term),
    ?line delete_files([Foo | Fs3]),

    ?line true = P0 =:= pps(),

    ?line BE1 = <<20000:32, 17:160000>>,
    ?line BE2 = <<20000:32, 1717:160000>>,
    ?line ok = file:write_file(Foo, [BE1,BE2,BE1,BE2]),
    ?line ok = file_sorter:sort([Foo], Foo, [{format,binary},
                                             {size,10000},
                                             {unique,true}]),
    ?line BEs = list_to_binary([BE1, BE2]),
    ?line {ok, BEs} = file:read_file(Foo),
    ?line delete_files(Foo),
    ?line true = P0 =:= pps(),

    ?line Fs4 = to_files([[7,4,1]], binary_term, Config),
    ?line {error, {bad_term, _}} = file_sorter:sort(Fs4, Foo, {format, term}),
    ?line delete_files([Foo | Fs4]),
    ?line true = P0 =:= pps(),

    ok.

%%% 
%%% Utilities.
%%% 

sort(Fmt, XArgs, Config) ->
    Args = make_args(Fmt, [{size,5} | XArgs]),
    TmpArgs = [{tmpdir,?privdir(Config)} | Args],
    Foo = outfile("foo", Config),

    %% Input is a fun. Output is a fun.
    ?line [] = file_sorter:sort(input([], 2, Fmt), output([], Fmt), Args),
    ?line L1 = [3,1,2,5,4],
    ?line S1 = file_sorter:sort(input(L1, 2, Fmt), output([], Fmt), TmpArgs),
    ?line S1 = rev(lists:sort(L1), TmpArgs),

    %% Input is a file. Output is a fun.
    ?line [] = file_sorter:sort([], output([], Fmt), Args),
    ?line L2 = [3,1,2,5,4],
    ?line Fs1 = to_files([L2], Fmt, Config),
    ?line S2 = file_sorter:sort(Fs1, output([], Fmt), TmpArgs),
    ?line S2 = rev(lists:sort(L2), TmpArgs),
    ?line delete_files(Fs1),

    %% Input is a file. Output is a file
    ?line ok = file_sorter:sort([], Foo, Args),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:sort([], Foo, [{unique,true} | Args]),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line L3 = [3,1,2,5,4,6],
    ?line Fs2 = to_files([L3], Fmt, Config),
    ?line ok = file_sorter:sort(Fs2, Foo, Args),
    ?line true = rev(lists:sort(L3), Args) =:= from_files(Foo, Fmt),
    ?line delete_files([Foo | Fs2]),
    ?line L4 = [1,3,4,1,2,5,4,5,6],
    ?line Fs3 = to_files([L4], Fmt, Config),
    ?line ok = file_sorter:sort(Fs3, Foo, Args++[{unique,true}, 
						 {size,100000}]),
    ?line true = rev(lists:usort(L4), Args) =:= from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:sort(Fs3, Foo, Args++[{unique,true}]),
    ?line true = rev(lists:usort(L4), Args) =:= from_files(Foo, Fmt),
    ?line delete_files([Foo | Fs3]),

    %% Input is a fun. Output is a file.
    ?line ok = file_sorter:sort(input([], 2, Fmt), Foo, Args),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line L5 = [3,1,2,5,4,7],
    ?line ok = file_sorter:sort(input(L5, 2, Fmt), Foo, Args),
    ?line true = rev(lists:sort(L5), Args) =:= from_files(Foo, Fmt),
    ?line delete_files(Foo),

    %% Removing duplicate keys.
    KFun = key_compare(2),
    L6 = [{5,e},{2,b},{3,c},{1,a},{4,d}] ++ [{2,c},{1,b},{4,a}],
    KUArgs = lists:keydelete(order, 1, Args) ++ 
             [{unique, true}, {order, KFun},{size,100000}],
    ?line ok = file_sorter:sort(input(L6, 2, Fmt), Foo, KUArgs),
    ?line true = rev(lists:ukeysort(2, L6), KUArgs) =:= from_files(Foo, Fmt),
    KArgs = lists:keydelete(unique, 1, KUArgs),
    ?line ok = file_sorter:sort(input(L6, 2, Fmt), Foo, KArgs),
    ?line true = rev(lists:keysort(2, L6), KArgs) =:= from_files(Foo, Fmt),

    %% Removing duplicate keys. Again.
    KUArgs2 = lists:keydelete(order, 1, Args) ++ 
              [{unique, true}, {order, KFun},{size,5}],
    ?line ok = file_sorter:sort(input(L6, 2, Fmt), Foo, KUArgs2),
    ?line true = rev(lists:ukeysort(2, L6), KUArgs2) =:= from_files(Foo, Fmt),
    KArgs2 = lists:keydelete(unique, 1, KUArgs2),
    ?line ok = file_sorter:sort(input(L6, 2, Fmt), Foo, KArgs2),
    ?line true = rev(lists:keysort(2, L6), KArgs2) =:= from_files(Foo, Fmt),
    ?line delete_files(Foo),
    
    ok.

keysort(Fmt, XArgs, Config) ->
    Args = make_args(Fmt, [{size,50}, {no_files, 2} | XArgs]),
    TmpArgs = Args ++ [{tmpdir,?privdir(Config)}],
    Foo = outfile("foo", Config),

    %% Input is files. Output is a file.
    ?line ok = file_sorter:keysort(2, [], Foo, Args),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keysort(2, [], Foo, [{unique,true} | Args]),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line L0 = [{a,2},{a,1},{a,2},{a,2},{a,1},{a,2},{a,2},{a,3}],
    ?line Fs0 = to_files([L0], Fmt, Config),
    ?line S = rev(lists:ukeysort(1, L0), Args),
    ?line ok = 
	file_sorter:keysort(1, Fs0, Foo, Args ++ [{unique,true},
						  {size,100000}]),
    ?line S = from_files(Foo, Fmt),
    ?line ok = 
	file_sorter:keysort(1, Fs0, Foo, Args ++ [{unique,true},
						  {size,5}]),
    ?line S = from_files(Foo, Fmt),
    ?line ok = file_sorter:keysort(1, Fs0, Foo, Args ++ [{unique,true}]),
    ?line S = from_files(Foo, Fmt),
    ?line delete_files([Foo | Fs0]),
    ?line L11 = [{a,1,x4},{b,2,x4},{c,3,x4}],
    ?line L21 = [{a,1,x3},{b,2,x3},{c,3,x3}],
    ?line L31 = [{a,1,x2},{b,2,x2},{c,3,x2}],
    ?line L41 = [{a,1,x1},{b,2,x1},{c,3,x1}],
    ?line All = [L11, L21, L31, L41],
    ?line AllFlat = lists:append(All),
    ?line Sorted = rev(lists:keysort(2, AllFlat), Args),
    ?line Fs1 = to_files(All, Fmt, Config),
    ?line ok = file_sorter:keysort(2, Fs1, Foo, Args),
    ?line Sorted = from_files(Foo, Fmt),
    ?line delete_files(Foo),

    %% Input is files. Output is a fun.
    ?line [] = file_sorter:keysort(2, [], output([], Fmt), Args),
    ?line KS1 = file_sorter:keysort(2, Fs1, output([], Fmt), TmpArgs),
    ?line Sorted = KS1,
    ?line delete_files(Fs1),

    %% Input is a fun. Output is a file.
    ?line ok = file_sorter:keysort(2, input([], 2, Fmt), Foo, Args),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keysort(2, input(AllFlat, 4, Fmt), Foo, Args),
    ?line Sorted = from_files(Foo,  Fmt),
    ?line delete_files(Foo),

    %% Input is a fun. Output is a fun.
    ?line [] = file_sorter:keysort(2, input([], 2, Fmt), output([], Fmt),Args),
    ?line KS2 = 
	file_sorter:keysort(2, input(AllFlat, 4, Fmt), output([], Fmt), 
			    TmpArgs),
    ?line Sorted = KS2,
    ok.

merge(Fmt, XArgs, Config) ->
    Args = make_args(Fmt, [{size,5} | XArgs]),
    Foo = outfile("foo", Config),

    %% Input is a file. Output is a fun.
    ?line [] = file_sorter:merge([], output([], Fmt), Args),
    ?line L2 = [[1,3,5],[2,4,5]],
    ?line Fs1 = to_files(L2, Fmt, Config),
    ?line S2 = file_sorter:sort(Fs1, output([], Fmt), Args),
    ?line S2 = rev(lists:sort(lists:append(L2)), Args),
    ?line delete_files(Fs1),

    %% Input is a file. Output is a file
    ?line ok = file_sorter:merge([], Foo, Args),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:merge([], Foo, [{unique,true} | Args]),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line L31 = [1,2,3],
    ?line L32 = [2,3,4],
    ?line L33 = [4,5,6],
    ?line L3r = [L31, L32, L33],
    ?line L3 = [rev(L31,Args), rev(L32,Args), rev(L33,Args)],
    ?line Fs2 = to_files(L3, Fmt, Config),
    ?line ok = file_sorter:merge(Fs2, Foo, Args),
    ?line true = rev(lists:merge(L3r), Args) =:= from_files(Foo, Fmt),
    ?line ok = file_sorter:merge(Fs2, Foo, Args++[{unique,true}, 
						  {size,100000}]),
    ?line true = rev(lists:umerge(L3r), Args) =:= from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:merge(Fs2, Foo, Args++[{unique,true}]),
    ?line true = rev(lists:umerge(L3r), Args) =:= from_files(Foo, Fmt),
    ?line delete_files([Foo | Fs2]),

    ok.

keymerge(Fmt, XArgs, Config) ->
    Args = make_args(Fmt, [{size,50}, {no_files, 2} | XArgs]),
    Foo = outfile("foo", Config),

    %% Input is files. Output is a file.
    ?line ok = file_sorter:keymerge(2, [], Foo, Args),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keymerge(2, [], Foo, [{unique,true} | Args]),
    ?line [] = from_files(Foo, Fmt),
    ?line delete_files(Foo),
    ?line L0 = [rev([{a,1},{a,2}], Args), rev([{a,2},{a,1},{a,3}], Args)],
    ?line Fs0 = to_files(L0, Fmt, Config),
    ?line delete_files(Foo),
    ?line ok = file_sorter:keymerge(1, Fs0, Foo, Args ++ [{unique,false}]),
    ?line S2 = rev([{a,1},{a,2},{a,2},{a,1},{a,3}], Args),
    ?line S2 = from_files(Foo, Fmt),
    ?line delete_files([Foo | Fs0]),
    ?line L11 = [{a,1,x4},{b,2,x4},{c,3,x4}],
    ?line L21 = [{a,1,x3},{b,2,x3},{c,3,x3}],
    ?line L31 = [{a,1,x2},{b,2,x2},{c,3,x2}],
    ?line L41 = [{a,1,x1},{b,2,x1},{c,3,x1}],
    ?line All = 
	[rev(L11, Args), rev(L21, Args), rev(L31, Args), rev(L41, Args)],
    ?line Merged1 = lists:keymerge(2, L11, L21),
    ?line Merged2 = lists:keymerge(2, L31, L41),
    ?line Merged = rev(lists:keymerge(2, Merged1, Merged2), Args),
    ?line Fs1 = to_files(All, Fmt, Config),
    ?line ok = file_sorter:keymerge(2, Fs1, Foo, Args),
    ?line Merged = from_files(Foo, Fmt),

    fun() -> 
    UArgs = [{unique,true} | Args],
    ?line UMerged1 = lists:ukeymerge(2, L11, L21),
    ?line UMerged2 = lists:ukeymerge(2, L31, L41),
    ?line UMerged = rev(lists:ukeymerge(2, UMerged1, UMerged2), Args),
    ?line ok = file_sorter:keymerge(2, Fs1, Foo, UArgs),
    ?line UMerged = from_files(Foo, Fmt),
    UArgs2 = make_args(Fmt, [{unique,true}, {size,50} | XArgs]),
    ?line ok = file_sorter:keymerge(2, Fs1, Foo, UArgs2),
    ?line UMerged = from_files(Foo, Fmt),
    ?line List = rev([{a,1,x4},{b,2,x4},{c,3,x4}], Args),
    ?line FsL = to_files([List], Fmt, Config),
    ?line ok = file_sorter:keymerge(2, FsL, Foo, UArgs),
    ?line List = from_files(Foo, Fmt),
    ?line List1 = [{a,1,x4},{b,2,x4},{c,3,x4}],
    ?line List2 = [{a,3,x4},{b,4,x4},{c,5,x4}],
    ?line FsLL = to_files([rev(List1, Args), rev(List2, Args)], Fmt, Config),
    ?line ok = file_sorter:keymerge(2, FsLL, Foo, UArgs),
    ?line List1_2 = rev(lists:ukeymerge(2, List1, List2), Args),
    ?line List1_2 = from_files(Foo, Fmt),
    ?line delete_files(Foo)
    end(),

    %% Input is files. Output is a fun.
    ?line Fs3 = to_files(All, Fmt, Config),
    ?line [] = file_sorter:keysort(2, [], output([], Fmt), Args),
    ?line KS1 = file_sorter:keymerge(2, Fs3, output([], Fmt), Args),
    ?line Merged = KS1,
    ?line delete_files([Foo | Fs3]),

    ?line L2 = [[{a,1}],[{a,2}],[{a,3}],[{a,4}],[{a,5}],[{a,6}],[{a,7}]],
    ?line Fs2 = to_files(L2, Fmt, Config),
    ?line M = file_sorter:keymerge(1, Fs2, output([], Fmt), Args),
    ?line M = rev(lists:append(L2), Args),
    ?line delete_files(Fs2),

    ?line LL1 = [{d,4},{e,5},{f,6}],
    ?line LL2 = [{a,1},{b,2},{c,3}],
    ?line LL3 = [{j,10},{k,11},{l,12}],
    ?line LL4 = [{g,7},{h,8},{i,9}],
    ?line LL5 = [{p,16},{q,17},{r,18}],
    ?line LL6 = [{m,13},{n,14},{o,15}],
    ?line LLAll = [rev(LL1, Args),rev(LL2, Args),rev(LL3, Args),
                   rev(LL4, Args),rev(LL5, Args),rev(LL6, Args)],
    ?line FsLL6 = to_files(LLAll, Fmt, Config),
    ?line LL = rev(lists:sort(lists:append(LLAll)), Args),
    ?line ok = file_sorter:keymerge(1, FsLL6, Foo, Args),
    ?line LL = from_files(Foo, Fmt),
    ?line ok = file_sorter:keymerge(1, FsLL6, Foo, [{unique,true} | Args]),
    ?line LL = from_files(Foo, Fmt),
    ?line delete_files([Foo | FsLL6]),

    ok.

funmerge(Fmt, XArgs, Config) ->
    KComp = key_compare(2),
    Args = make_args(Fmt, [{order,KComp},{size,5}, {no_files, 5} | XArgs]),
    UArgs = [{unique,true} | Args],
    Foo = outfile(foo, Config),

    ?line EFs = to_files([[]], Fmt, Config),
    ?line ok = file_sorter:merge(EFs, Foo, UArgs),
    ?line [] = from_files(Foo, Fmt),
    delete_files([Foo | EFs]),

    ?line L11 = [{a,1,x4},{b,2,x4},{c,3,x4}],
    ?line L21 = [{a,1,x3},{b,2,x3},{c,3,x3}],
    ?line L31 = [{a,1,x2},{b,2,x2},{c,3,x2}],
    ?line L41 = [{a,1,x1},{b,2,x1},{c,3,x1}],
    ?line CAll = [L11, L21, L31, L41],
    ?line CMerged1 = lists:merge(KComp, L11, L21),
    ?line CMerged2 = lists:merge(KComp, L31, L41),
    ?line CMerged = lists:merge(KComp, CMerged1, CMerged2),
    ?line CFs1 = to_files(CAll, Fmt, Config),
    ?line ok = file_sorter:merge(CFs1, Foo, Args),
    ?line CMerged = from_files(Foo, Fmt),

    Args4 = make_args(Fmt, [{size,50} | XArgs]),
    ?line ok = file_sorter:merge(CFs1, Foo, [{order,KComp} | Args4]),
    ?line CMerged = from_files(Foo, Fmt),

    ?line UMerged1 = lists:umerge(KComp, L11, L21),
    ?line UMerged2 = lists:umerge(KComp, L31, L41),
    ?line UMerged = lists:umerge(KComp, UMerged1, UMerged2),
    ?line ok = file_sorter:merge(CFs1, Foo, [{order,KComp} | UArgs]),
    ?line UMerged = from_files(Foo, Fmt),
    UArgs2 = 
        lists:keydelete(order, 1,
                        make_args(Fmt, [{unique,true}, {size,50} | XArgs])),
    ?line ok = file_sorter:merge(CFs1, Foo, [{order,KComp} | UArgs2]),
    ?line UMerged = from_files(Foo, Fmt),
    ?line delete_files(Foo),

    ?line List1 = [{a,1,x4},{b,2,x4},{c,3,x4}],
    ?line List2 = [{a,3,x4},{b,4,x4},{c,5,x4}],
    ?line List3 = [{a,5,x4},{b,6,x4},{c,7,x4}],
    ?line FsLL = to_files([List1, List2, List3], Fmt, Config),
    ?line ok = file_sorter:merge(FsLL, Foo, Args),
    ?line List1_2 = lists:merge(KComp,lists:merge(KComp,List1,List2),List3),
    ?line List1_2 = from_files(Foo, Fmt),
    ?line ok = file_sorter:merge(FsLL, Foo, [{order,KComp} | UArgs]),
    ?line UList1_2 = 
        lists:umerge(KComp,lists:umerge(KComp, List1, List2),List3),
    ?line UList1_2 = from_files(Foo, Fmt),
    ?line delete_files([Foo | CFs1]),

    fun() ->
    ?line LL1 = [{d,4},{e,5},{f,6}],
    ?line LL2 = [{a,1},{b,2},{c,3}],
    ?line LL3 = [{j,10},{k,11},{l,12}],
    ?line LL4 = [{g,7},{h,8},{i,9}],
    ?line LL5 = [{p,16},{q,17},{r,18}],
    ?line LL6 = [{m,13},{n,14},{o,15}],
    ?line LLAll = [LL1,LL2,LL3,LL4,LL5,LL6],
    ?line FsLL6 = to_files(LLAll, Fmt, Config),
    ?line LL = lists:sort(lists:append(LLAll)),
    ?line ok = file_sorter:merge(FsLL6, Foo, Args),
    ?line LL = from_files(Foo, Fmt),
    ?line ok = file_sorter:merge(FsLL6, Foo, UArgs),
    ?line LL = from_files(Foo, Fmt),
    ?line delete_files([Foo | FsLL6])
    end(),

    fun() ->
    ?line RLL1 = [{b,2},{h,8},{n,14}],
    ?line RLL2 = [{a,1},{g,7},{m,13}],
    ?line RLL3 = [{d,4},{j,10},{p,16}],
    ?line RLL4 = [{c,3},{i,9},{o,15}],
    ?line RLL5 = [{f,6},{l,12},{r,18}],
    ?line RLL6 = [{e,5},{k,11},{q,17}],
    ?line RLLAll = [RLL1,RLL2,RLL3,RLL4,RLL5,RLL6],
    ?line RFsLL6 = to_files(RLLAll, Fmt, Config),
    ?line RLL = lists:sort(lists:append(RLLAll)),
    ?line ok = file_sorter:merge(RFsLL6, Foo, Args),
    ?line RLL = from_files(Foo, Fmt),
    ?line ok = file_sorter:merge(RFsLL6, Foo, UArgs),
    ?line RLL = from_files(Foo, Fmt),
    ?line delete_files([Foo | RFsLL6])
    end(),

    ok.

check(Fmt, Config) ->
    Args0 = make_args(Fmt, [{size,5}]),
    Args = Args0 ++ [{tmpdir,?privdir(Config)}],

    Fun = fun compare/2,

    L1 = [3,1,2,5,4],
    [F1_0] = Fs1 = to_files([L1], Fmt, Config),
    F1 = filename:absname(F1_0),
    ?line {ok, [{F1,2,1}]} = file_sorter:check(Fs1, Args),
    ?line {ok, [{F1,2,1}]} = file_sorter:check(Fs1, [{order,Fun} | Args]),
    ?line {ok, [{F1,2,1}]} = file_sorter:check(Fs1, [{unique,true} | Args]),
    ?line {ok, [{F1,2,1}]} = 
	file_sorter:check(Fs1, [{order,Fun},{unique,true} | Args]),
    ?line {ok, [{F1,3,2}]} = 
	file_sorter:check(Fs1, [{order,descending} | Args]),
    ?line {ok, [{F1,3,2}]} = 
	file_sorter:check(Fs1, [{unique,true},{order,descending} | Args]),
    ?line delete_files(Fs1),
    
    L2 = [[1,2,2,3,3,4,5,5],[5,5,4,3,3,2,2,1]],
    [F2_0,F3_0] = Fs2 = to_files(L2, Fmt, Config),
    F2 = filename:absname(F2_0),
    F3 = filename:absname(F3_0),
    ?line {ok, [{F3,3,4}]} = file_sorter:check(Fs2, Args),
    ?line {ok, [{F3,3,4}]} = file_sorter:check(Fs2, [{order,Fun} | Args]),
    ?line {ok, [{F2,3,2},{F3,2,5}]} = 
	file_sorter:check(Fs2, [{unique, true} | Args]),
    ?line {ok, [{F2,3,2},{F3,2,5}]} = 
	file_sorter:check(Fs2, [{order,Fun},{unique, true} | Args]),
    ?line {ok, [{F2,2,2}]} = 
	file_sorter:check(Fs2, [{order,descending} | Args]),
    ?line {ok, [{F2,2,2},{F3,2,5}]} = 
	file_sorter:check(Fs2, [{unique,true},{order,descending} | Args]),
    ?line delete_files(Fs2),
    
    L3 = [1,2,3,4],
    ?line Fs3 = to_files([L3], Fmt, Config),
    ?line {ok, []} = file_sorter:check(Fs3, [{unique,true} | Args]),
    ?line {ok, []} = 
	file_sorter:check(Fs3, [{unique,true},{order,Fun} | Args]),
    ?line delete_files(Fs3),

    %% big objects
    ?line T1 = erlang:make_tuple(10000,foo),
    ?line T2 = erlang:make_tuple(10000,bar),
    ?line L4 = [T1,T2],
    ?line [FF_0] = Fs4 = to_files([L4], Fmt, Config),
    FF = filename:absname(FF_0),
    ?line {ok, [{FF,2,T2}]} = file_sorter:check(Fs4, [{unique,true} | Args]),
    ?line delete_files(Fs4),

    CFun = key_compare(2),
    L10 = [[{1,a},{2,b},T10_1={1,b},{3,c}], [{1,b},T10_2={2,a}]],
    [F10_0,F11_0] = Fs10 = to_files(L10, Fmt, Config),
    F10_1 = filename:absname(F10_0),
    F11_1 = filename:absname(F11_0),
    ?line {ok, [{F10_1,3,T10_1},{F11_1,2,T10_2}]} = 
        file_sorter:check(Fs10, [{unique,true},{order,CFun} | Args]),
    ?line delete_files(Fs10),

    ok.

keycheck(Fmt, Config) ->
    Args0 = make_args(Fmt, [{size,5}]),
    Args = Args0 ++ [{tmpdir,?privdir(Config)}],

    ?line L1 = [[{a,1},{b,2}], [{c,2},{b,1},{a,3}]],
    ?line [F1_0,F2_0] = Fs1 = to_files(L1, Fmt, Config),
    F1 = filename:absname(F1_0),
    F2 = filename:absname(F2_0),
    ?line {ok, [{F2,2,{b,1}}]} = file_sorter:keycheck(1, Fs1, Args),
    ?line {ok, [{F2,2,{b,1}}]} = 
	file_sorter:keycheck(1, Fs1, [{unique,true} | Args]),
    ?line {ok, [{F1,2,{b,2}}]} = 
	file_sorter:keycheck(1, Fs1, [{order,descending},{unique,true} | Args]),
    ?line delete_files(Fs1),
    
    L2 = [[{a,1},{a,2},{a,2},{b,2}], [{c,2},{b,1},{b,2},{b,2},{a,3}]],
    ?line [F3_0,F4_0] = Fs2 = to_files(L2, Fmt, Config),
    F3 = filename:absname(F3_0),
    F4 = filename:absname(F4_0),
    ?line {ok, [{F4,2,{b,1}}]} = file_sorter:keycheck(1, Fs2, Args),
    ?line {ok, [{F3,2,{a,2}},{F4,2,{b,1}}]} = 
	file_sorter:keycheck(1, Fs2, [{unique,true} | Args]),
    ?line {ok, [{F3,4,{b,2}}]} = 
	file_sorter:keycheck(1, Fs2, [{order,descending} | Args]),
    ?line {ok, [{F3,2,{a,2}},{F4,3,{b,2}}]} = 
	file_sorter:keycheck(1, Fs2, 
			     [{order,descending},{unique,true} | Args]),
    ?line delete_files(Fs2),
    
    ok.

rev(L, Args) ->
    case lists:member({order, descending}, Args) of
	true ->
	    lists:reverse(L);
	false ->
	    L
    end.

make_args({HL, Fmt}, Args) ->
    make_args(Fmt, [{header, HL} | Args]);
make_args(Fmt, Args) ->
    [{format, Fmt} | Args].

compare(X, Y) ->
    X =< Y.

key_compare(I) ->
    fun(X, Y) -> 
            element(I, bin_to_term(X)) =< element(I, bin_to_term(Y))
    end.

bin_to_term(B) when is_binary(B) -> binary_to_term(B);
bin_to_term(T) -> T.

-define(CHUNKSIZE, 8096).

pps() ->
    erlang:ports().

input(L, N, term) ->
    input(L, N);
input(L, N, {_HL, Format}) when Format =:= binary_term; Format =:= binary ->
    binput(L, N);
input(L, N, Format) when Format =:= binary_term; Format =:= binary ->
    binput(L, N).

binput(L, N) ->
    Bs = lists:map(fun(T) -> term_to_binary(T) end, L),
    input(Bs, N).

input(L, N) ->
    fun(close) ->
	    ok;
       (read) ->
	    case L of
		[] -> end_of_input;
		_ ->
		    R = lists:sublist(L, N),
		    NL = lists:nthtail(length(R), L),
		    {R, input(NL, N)}
	    end
    end.

output(L, term) ->
    output(L);
output(L, {_HL, Format}) when Format =:= binary_term; Format =:= binary ->
    boutput(L);
output(L, Format) when Format =:= binary_term; Format =:= binary ->
    boutput(L).

output(A) ->
    fun(close) -> 
	    lists:append(lists:reverse(A));
       (L) when is_list(L) ->
	    output([L | A])
    end.

boutput(A) ->
    fun(close) -> 
	    Bs = lists:append(lists:reverse(A)),
	    lists:map(fun(B) -> binary_to_term(B) end, Bs);
       (L) when is_list(L) ->
	    boutput([L | A])
    end.

outfile(Name, Config) ->
    list_to_atom(filename:join(?privdir(Config), Name)).

%% [[term()]] -> [filename()]
to_files(Lists, term, Config) ->
    terms_to_files(Lists, Config);
to_files(Lists, Format, Config) when Format =:= binary_term; 
				     Format =:= binary ->
    bins_to_files(Lists, 4, Config);
to_files(Lists, {HL, Format}, Config) when Format =:= binary_term; 
					   Format =:= binary ->
    bins_to_files(Lists, HL, Config).

%% [[term()]] -> [filename()]
terms_to_files(Lists, Config) ->
    PrivDir = ?privdir(Config),
    terms_to_files(Lists, PrivDir, 1).

terms_to_files([L | Ls], PrivDir, N) ->
    F = lists:concat([?MODULE, '_', N]),
    File = filename:join(PrivDir, F),
    {ok, Fd} = file:open(File, [write]),
    write_terms(Fd, L),
    file:close(Fd),
    [list_to_atom(File) | terms_to_files(Ls, PrivDir, N+1)];
terms_to_files([], _PrivDir, _N) ->
    [].

write_terms(Fd, [T | Ts]) ->
    io:format(Fd, "~p.~n", [T]),
    write_terms(Fd, Ts);
write_terms(_Fd, []) ->
    ok.

%% [[term()]] -> [filename()]
bins_to_files(Lists, HL, Config) ->
    PrivDir = ?privdir(Config),
    bins_to_files(Lists, PrivDir, 1, HL).

bins_to_files([L | Fs], PrivDir, N, HL) ->
    F = lists:concat([?MODULE, '_', N]),
    File = filename:join(PrivDir, F),
    {ok, Fd} = file:open(File, [raw,binary,write]),
    write_bins(Fd, L, HL),
    file:close(Fd),
    [list_to_atom(File) | bins_to_files(Fs, PrivDir, N+1, HL)];
bins_to_files([], _PrivDir, _N, _HL) ->
    [].

write_bins(Fd, [T | Ts], HL) ->
    B = term_to_binary(T),
    Sz = byte_size(B),
    ok = file:write(Fd, [<<Sz:HL/unit:8>>, B]),
    write_bins(Fd, Ts, HL);
write_bins(_Fd, [], _HL) ->
    ok.

%% [filename()] -> [[term()]] or filename() -> [term()]
from_files(Files, term) ->
    terms_from_files(Files);
from_files(Files, Format) when Format =:= binary_term; Format =:= binary ->
    bins_from_files(Files, 4);
from_files(Files, {HL, Format}) when Format =:= binary_term; 
                                     Format =:= binary ->
    bins_from_files(Files, HL).

%% [filename()] -> [[term()]] or filename() -> [term()]
terms_from_files(File) when is_atom(File) ->
    [Terms] = terms_from_files([File]),
    Terms;
terms_from_files(Files) ->
    lists:map(fun(F) -> terms_from_file(F) end, Files).

terms_from_file(File) ->
    {ok, Fd} = file:open(File, [read,compressed]),
    terms_from_file(Fd, []).

terms_from_file(Fd, L) ->
    case io:read(Fd, '') of
	{ok, Term} ->
	    terms_from_file(Fd, [Term | L]);
	eof ->
	    file:close(Fd),
	    lists:reverse(L)
    end.

%% [filename()] -> [[term()]]
bins_from_files(File, HL) when is_atom(File) ->
    [Bins] = bins_from_files([File], HL),
    Bins;
bins_from_files(Files, HL) ->
    lists:map(fun(F) -> collect(F, HL) end, Files).

delete_files(File) when is_atom(File) ->
    file:delete(File);
delete_files(Files) ->
    lists:foreach(fun(F) -> file:delete(F) end, Files).

%%%
%%% Collects binaries converted to terms in a list. Not very efficient.
%%%
collect(F, HL) ->
    {ok, Fd} = file:open(F, [read, binary, raw, compressed]),
    R = (catch c(Fd, <<>>, 0, ?CHUNKSIZE, HL, [])),
    file:close(Fd),
    R.

c(Fd, Bin0, Size0, NoBytes, HL, L) ->
    case file:read(Fd, NoBytes) of
	{ok, Bin} ->
	    Size = Size0 + byte_size(Bin),
	    NBin = list_to_binary([Bin0, Bin]),
	    c1(Fd, NBin, Size, HL, L);
	eof when Size0 =:= 0 ->
	    lists:reverse(L);
        eof ->
	    test_server:fail({error, premature_eof});
	Error ->
	    test_server:fail(Error)
    end.

c1(Fd, B, BinSize, HL, L) -> 
    case B of 
	<<Size:HL/unit:8, Bin/binary>> ->
	    if 
		Size > BinSize - HL, Size > ?CHUNKSIZE ->
		    c(Fd, B, BinSize, Size + HL, HL, L);
		Size > BinSize - HL ->
		    c(Fd, B, BinSize, ?CHUNKSIZE, HL, L);
		true ->
		    <<BinTerm:Size/binary, R/binary>> = Bin,	    
		    E = case catch binary_to_term(BinTerm) of
                            {'EXIT', _} ->
				test_server:fail({error, bad_object});
			    Term ->
				Term
			end,
		    NBinSize = BinSize - HL - Size,
		    c1(Fd, R, NBinSize, HL, [E | L])
	    end;
	_ ->
	    c(Fd, B, BinSize, ?CHUNKSIZE, HL, L)
    end.