xref: /5.5.2/couchdb/test/etap/040-util.t (revision 9f12dd5b)
1#!/usr/bin/env escript
2%% -*- erlang -*-
3%%! -smp enable
4
5% Licensed under the Apache License, Version 2.0 (the "License"); you may not
6% use this file except in compliance with the License. You may obtain a copy of
7% the License at
8%
9%   http://www.apache.org/licenses/LICENSE-2.0
10%
11% Unless required by applicable law or agreed to in writing, software
12% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14% License for the specific language governing permissions and limitations under
15% the License.
16
17main(_) ->
18    test_util:init_code_path(),
19    application:start(crypto),
20
21    etap:plan(14),
22    case (catch test()) of
23        ok ->
24            etap:end_tests();
25        Other ->
26            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
27            etap:bail(Other)
28    end,
29    ok.
30
31test() ->
32    % to_existing_atom
33    etap:is(true, couch_util:to_existing_atom(true), "An atom is an atom."),
34    etap:is(foo, couch_util:to_existing_atom(<<"foo">>),
35        "A binary foo is the atom foo."),
36    etap:is(foobarbaz, couch_util:to_existing_atom("foobarbaz"),
37        "A list of atoms is one munged atom."),
38
39    % implode
40    etap:is([1, 38, 2, 38, 3], couch_util:implode([1,2,3],"&"),
41        "use & as separator in list."),
42
43    % trim
44    Strings = [" foo", "foo ", "\tfoo", " foo ", "foo\t", "foo\n", "\nfoo"],
45    etap:ok(lists:all(fun(S) -> couch_util:trim(S) == "foo" end, Strings),
46        "everything here trimmed should be foo."),
47
48    % abs_pathname
49    {ok, Cwd} = file:get_cwd(),
50    etap:is(string:to_lower(Cwd) ++ "/foo", string:to_lower(couch_util:abs_pathname("./foo")),
51        "foo is in this directory."),
52
53    % should_flush
54    etap:ok(not couch_util:should_flush(),
55        "Not using enough memory to flush."),
56    AcquireMem = fun() ->
57        IntsToAGazillion = lists:seq(1, 200000),
58        LotsOfData = lists:map(
59            fun(Int) -> {Int, <<"foobar">>} end,
60        lists:seq(1, 500000)),
61        etap:ok(couch_util:should_flush(),
62            "Allocation 200K tuples puts us above the memory threshold.")
63    end,
64    AcquireMem(),
65
66    etap:ok(not couch_util:should_flush(),
67        "Checking to flush invokes GC."),
68
69    % verify
70    etap:is(true, couch_util:verify("It4Vooya", "It4Vooya"),
71         "String comparison."),
72    etap:is(false, couch_util:verify("It4VooyaX", "It4Vooya"),
73         "String comparison (unequal lengths)."),
74    etap:is(true, couch_util:verify(<<"ahBase3r">>, <<"ahBase3r">>),
75        "Binary comparison."),
76    etap:is(false, couch_util:verify(<<"ahBase3rX">>, <<"ahBase3r">>),
77        "Binary comparison (unequal lengths)."),
78    etap:is(false, couch_util:verify(nil, <<"ahBase3r">>),
79        "Binary comparison with atom."),
80
81    ok.
82