-module('prod_MapTest_SUITE').

-include_lib("common_test/include/ct.hrl").

-export([
    all/0,
    groups/0,
    suite/0
]).

-export([
    init_per_suite/1,
    init_per_group/2,
    end_per_group/2,
    end_per_suite/1
]).

-export(['MapTest-001'/1]).
-export(['MapTest-002'/1]).
-export(['MapTest-003'/1]).
-export(['MapTest-004'/1]).
-export(['MapTest-005'/1]).
-export(['MapTest-006'/1]).
-export(['MapTest-007'/1]).
-export(['MapTest-008'/1]).
-export(['MapTest-009'/1]).
-export(['MapTest-010'/1]).
-export(['MapTest-011'/1]).
-export(['MapTest-012'/1]).
-export(['MapTest-013'/1]).
-export(['MapTest-014'/1]).
-export(['MapTest-015'/1]).
-export(['MapTest-016'/1]).
-export(['MapTest-017'/1]).
-export(['MapTest-018'/1]).
-export(['MapTest-019'/1]).
-export(['MapTest-020'/1]).
-export(['MapTest-040'/1]).
-export(['MapTest-041'/1]).
-export(['MapTest-042'/1]).
-export(['MapTest-043'/1]).
-export(['MapTest-044'/1]).
-export(['MapTest-045'/1]).
-export(['MapTest-046'/1]).
-export(['MapTest-047'/1]).
-export(['MapTest-048'/1]).
-export(['MapTest-049'/1]).
-export(['MapTest-050'/1]).
-export(['MapTest-051'/1]).
-export(['MapTest-052'/1]).
-export(['MapTest-053'/1]).
-export(['MapTest-054'/1]).
-export(['MapTest-055'/1]).
-export(['MapTest-057'/1]).

suite() -> [{timetrap, {seconds, 180}}].

init_per_group(_, Config) -> Config.

end_per_group(_, _Config) ->
    xqerl_code_server:unload(all).

end_per_suite(_Config) ->
    ct:timetrap({seconds, 60}),
    xqerl_code_server:unload(all).

init_per_suite(Config) ->
    {ok, _} = application:ensure_all_started(xqerl),
    DD = filename:dirname(filename:dirname(filename:dirname(?config(data_dir, Config)))),
    TD = filename:join(DD, "QT3-test-suite"),
    __BaseDir = filename:join(TD, "prod"),
    [{base_dir, __BaseDir} | Config].

all() ->
    [
        {group, group_0},
        {group, group_1}
    ].

groups() ->
    [
        {group_0, [parallel], [
            'MapTest-001',
            'MapTest-002',
            'MapTest-003',
            'MapTest-004',
            'MapTest-005',
            'MapTest-006',
            'MapTest-007',
            'MapTest-008',
            'MapTest-009',
            'MapTest-010',
            'MapTest-011',
            'MapTest-012',
            'MapTest-013',
            'MapTest-014',
            'MapTest-015',
            'MapTest-016',
            'MapTest-017',
            'MapTest-018',
            'MapTest-019',
            'MapTest-020',
            'MapTest-040',
            'MapTest-041',
            'MapTest-042'
        ]},
        {group_1, [parallel], [
            'MapTest-043',
            'MapTest-044',
            'MapTest-045',
            'MapTest-046',
            'MapTest-047',
            'MapTest-048',
            'MapTest-049',
            'MapTest-050',
            'MapTest-051',
            'MapTest-052',
            'MapTest-053',
            'MapTest-054',
            'MapTest-055',
            'MapTest-057'
        ]}
    ].

environment('map', __BaseDir) ->
    [
        {'decimal-formats', []},
        {sources, []},
        {collections, []},
        {'static-base-uri', []},
        {params, []},
        {vars, []},
        {namespaces, [{"-functions/map", "map"}]},
        {schemas, []},
        {resources, []},
        {modules, []}
    ].

'MapTest-001'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{} instance of map(*)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-001.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-002'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(*)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-002.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-003'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\", \"London\":1} instance of map(*)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-003.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-004'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{} instance of map(xs:integer, xs:string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-004.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-005'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(xs:integer, xs:string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-005.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-006'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\", \"London\":1} instance of map(xs:integer, xs:string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-006.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-007'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(xs:integer)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-007.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPST0003") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPST0003 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-008'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(integer, string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-008.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPST0051") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPST0051 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-009'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map:remove(map{1:\"London\", \"London\":1}, \"London\") instance of map(xs:integer, xs:string)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-009.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-010'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map:put(map{1:\"London\", 2:\"Paris\"}, 3, current-date()) instance of map(xs:integer, xs:string)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-010.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-011'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map:put(map{1:\"London\", 2:\"Paris\"}, 3, \"Berlin\") instance of map(xs:decimal, xs:string+)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-011.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-012'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":(), \"Paris\":(), \"Berlin\":()} instance of map(xs:string, empty-sequence())",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-012.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-013'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":(), \"Paris\":(), \"Berlin\":(), \"Rome\":5} instance of map(xs:string, empty-sequence())",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-013.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-014'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":()} instance of map(xs:string, xs:integer?)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-014.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-015'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":()} instance of map(xs:string, xs:integer+)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-015.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-016'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5} instance of map(xs:string+, xs:integer+)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-016.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPST0003") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPST0003 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-017'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5} instance of map(xs:string, xs:integer+)*",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-017.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-018'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "(map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5}, map{}) instance of map(xs:string, xs:integer+)*",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-018.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-019'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "(map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5}, map{}) instance of map(xs:string, xs:integer+)?",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-019.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-020'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         let $f := function($m as map(xs:integer, xs:integer)) as xs:boolean {\n"
        "             map:contains($m, 17)\n"
        "         }\n"
        "         return $f(map{17: xs:untypedAtomic('234')})\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-020.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPTY0004") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPTY0004 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-040'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(*)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-040.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-041'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:decimal, xs:string+)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-041.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-042'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(*)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(*)) as xs:integer",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-042.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-043'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(*)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-043.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-044'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(xs:anyAtomicType) as item()*) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-044.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-045'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(xs:integer) as item()*) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-045.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-046'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         map{} instance of map(xs:integer, map(xs:integer, xs:string))\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-046.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-047'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         let $f := function() as map(xs:integer, xs:string) {map{}},\n"
        "             $g := function($m as map(xs:string, xs:integer)) as xs:boolean {map:size($m) = 0}\n"
        "         return $g($f())\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-047.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-048'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         let $f := function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         return $f(map{})\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-048.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_eq(Res, "0") of
            true -> {comment, "Equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-049'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         declare function local:f ($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)};\n"
        "         let $g as map(xs:string, xs:date) := map:remove(map{'a':current-date()}, 'a') \n"
        "         return local:f($g)\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-049.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_eq(Res, "0") of
            true -> {comment, "Equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-050'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(*)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-050.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-051'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:decimal, xs:string+)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-051.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-052'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as function(*)) as xs:integer {function-arity($m)} \n"
        "         instance of function(map(*)) as xs:integer",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-052.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-053'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as function(*)) as xs:integer {function-arity($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-053.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-054'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as function(xs:anyAtomicType) as item()*) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-054.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-055'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:2, 2:4, 3:6}(2)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-055.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_eq(Res, "4") of
            true -> {comment, "Equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-057'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "let $map:= map{1:'a', 2:'b', 3:'c', 4:'d'} return $map(3)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-057.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_deep_eq(Res, "'c'") of
            true -> {comment, "Deep equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

-module('prod_MapTest_SUITE').

-include_lib("common_test/include/ct.hrl").

-export([
    all/0,
    groups/0,
    suite/0
]).

-export([
    init_per_suite/1,
    init_per_group/2,
    end_per_group/2,
    end_per_suite/1
]).

-export(['MapTest-001'/1]).
-export(['MapTest-002'/1]).
-export(['MapTest-003'/1]).
-export(['MapTest-004'/1]).
-export(['MapTest-005'/1]).
-export(['MapTest-006'/1]).
-export(['MapTest-007'/1]).
-export(['MapTest-008'/1]).
-export(['MapTest-009'/1]).
-export(['MapTest-010'/1]).
-export(['MapTest-011'/1]).
-export(['MapTest-012'/1]).
-export(['MapTest-013'/1]).
-export(['MapTest-014'/1]).
-export(['MapTest-015'/1]).
-export(['MapTest-016'/1]).
-export(['MapTest-017'/1]).
-export(['MapTest-018'/1]).
-export(['MapTest-019'/1]).
-export(['MapTest-020'/1]).
-export(['MapTest-040'/1]).
-export(['MapTest-041'/1]).
-export(['MapTest-042'/1]).
-export(['MapTest-043'/1]).
-export(['MapTest-044'/1]).
-export(['MapTest-045'/1]).
-export(['MapTest-046'/1]).
-export(['MapTest-047'/1]).
-export(['MapTest-048'/1]).
-export(['MapTest-049'/1]).
-export(['MapTest-050'/1]).
-export(['MapTest-051'/1]).
-export(['MapTest-052'/1]).
-export(['MapTest-053'/1]).
-export(['MapTest-054'/1]).
-export(['MapTest-055'/1]).
-export(['MapTest-057'/1]).

suite() -> [{timetrap, {seconds, 180}}].

init_per_group(_, Config) -> Config.

end_per_group(_, _Config) ->
    xqerl_code_server:unload(all).

end_per_suite(_Config) ->
    ct:timetrap({seconds, 60}),
    xqerl_code_server:unload(all).

init_per_suite(Config) ->
    {ok, _} = application:ensure_all_started(xqerl),
    DD = filename:dirname(filename:dirname(filename:dirname(?config(data_dir, Config)))),
    TD = filename:join(DD, "QT3-test-suite"),
    __BaseDir = filename:join(TD, "prod"),
    [{base_dir, __BaseDir} | Config].

all() ->
    [
        {group, group_0},
        {group, group_1}
    ].

groups() ->
    [
        {group_0, [parallel], [
            'MapTest-001',
            'MapTest-002',
            'MapTest-003',
            'MapTest-004',
            'MapTest-005',
            'MapTest-006',
            'MapTest-007',
            'MapTest-008',
            'MapTest-009',
            'MapTest-010',
            'MapTest-011',
            'MapTest-012',
            'MapTest-013',
            'MapTest-014',
            'MapTest-015',
            'MapTest-016',
            'MapTest-017',
            'MapTest-018',
            'MapTest-019',
            'MapTest-020',
            'MapTest-040',
            'MapTest-041',
            'MapTest-042'
        ]},
        {group_1, [parallel], [
            'MapTest-043',
            'MapTest-044',
            'MapTest-045',
            'MapTest-046',
            'MapTest-047',
            'MapTest-048',
            'MapTest-049',
            'MapTest-050',
            'MapTest-051',
            'MapTest-052',
            'MapTest-053',
            'MapTest-054',
            'MapTest-055',
            'MapTest-057'
        ]}
    ].

environment('map', __BaseDir) ->
    [
        {'decimal-formats', []},
        {sources, []},
        {collections, []},
        {'static-base-uri', []},
        {params, []},
        {vars, []},
        {namespaces, [{"-functions/map", "map"}]},
        {schemas, []},
        {resources, []},
        {modules, []}
    ].

'MapTest-001'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{} instance of map(*)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-001.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-002'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(*)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-002.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-003'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\", \"London\":1} instance of map(*)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-003.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-004'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{} instance of map(xs:integer, xs:string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-004.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-005'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(xs:integer, xs:string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-005.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-006'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\", \"London\":1} instance of map(xs:integer, xs:string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-006.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-007'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(xs:integer)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-007.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPST0003") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPST0003 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-008'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:\"London\"} instance of map(integer, string)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-008.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPST0051") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPST0051 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-009'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map:remove(map{1:\"London\", \"London\":1}, \"London\") instance of map(xs:integer, xs:string)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-009.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-010'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map:put(map{1:\"London\", 2:\"Paris\"}, 3, current-date()) instance of map(xs:integer, xs:string)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-010.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-011'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map:put(map{1:\"London\", 2:\"Paris\"}, 3, \"Berlin\") instance of map(xs:decimal, xs:string+)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-011.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-012'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":(), \"Paris\":(), \"Berlin\":()} instance of map(xs:string, empty-sequence())",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-012.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-013'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":(), \"Paris\":(), \"Berlin\":(), \"Rome\":5} instance of map(xs:string, empty-sequence())",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-013.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-014'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":()} instance of map(xs:string, xs:integer?)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-014.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-015'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":()} instance of map(xs:string, xs:integer+)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-015.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-016'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5} instance of map(xs:string+, xs:integer+)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-016.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPST0003") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPST0003 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-017'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5} instance of map(xs:string, xs:integer+)*",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-017.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-018'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "(map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5}, map{}) instance of map(xs:string, xs:integer+)*",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-018.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-019'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "(map{\"London\":1, \"Paris\":2, \"Berlin\":3, \"Rome\":5}, map{}) instance of map(xs:string, xs:integer+)?",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-019.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-020'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         let $f := function($m as map(xs:integer, xs:integer)) as xs:boolean {\n"
        "             map:contains($m, 17)\n"
        "         }\n"
        "         return $f(map{17: xs:untypedAtomic('234')})\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-020.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_error(Res, "XPTY0004") of
            true -> {comment, "Correct error"};
            {true, F} -> {comment, "WE: XPTY0004 " ++ binary_to_list(F)};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-040'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(*)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-040.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-041'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:decimal, xs:string+)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-041.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-042'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(*)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(*)) as xs:integer",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-042.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-043'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(*)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-043.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-044'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(xs:anyAtomicType) as item()*) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-044.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-045'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         instance of function(function(xs:integer) as item()*) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-045.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_false(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-046'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         map{} instance of map(xs:integer, map(xs:integer, xs:string))\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-046.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-047'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         let $f := function() as map(xs:integer, xs:string) {map{}},\n"
        "             $g := function($m as map(xs:string, xs:integer)) as xs:boolean {map:size($m) = 0}\n"
        "         return $g($f())\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-047.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-048'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         let $f := function($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)} \n"
        "         return $f(map{})\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-048.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_eq(Res, "0") of
            true -> {comment, "Equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-049'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         declare function local:f ($m as map(xs:integer, xs:string)) as xs:integer {map:size($m)};\n"
        "         let $g as map(xs:string, xs:date) := map:remove(map{'a':current-date()}, 'a') \n"
        "         return local:f($g)\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-049.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_eq(Res, "0") of
            true -> {comment, "Equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-050'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(*)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-050.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-051'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as map(xs:decimal, xs:string+)) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-051.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-052'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as function(*)) as xs:integer {function-arity($m)} \n"
        "         instance of function(map(*)) as xs:integer",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-052.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-053'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as function(*)) as xs:integer {function-arity($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-053.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-054'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry =
        "\n"
        "         function($m as function(xs:anyAtomicType) as item()*) as xs:integer {map:size($m)} \n"
        "         instance of function(map(xs:integer, xs:string)) as xs:integer\n"
        "      ",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-054.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_true(Res) of
            true -> {comment, "Empty"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-055'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "map{1:2, 2:4, 3:6}(2)",
    Qry1 = Qry,
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-055.xq"), Qry1),
            xqerl:run(Mod)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_eq(Res, "4") of
            true -> {comment, "Equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.

'MapTest-057'(Config) ->
    __BaseDir = ?config(base_dir, Config),
    Qry = "let $map:= map{1:'a', 2:'b', 3:'c', 4:'d'} return $map(3)",
    {Env, Opts} = xqerl_test:handle_environment(environment('map', __BaseDir)),
    Qry1 = lists:flatten(Env ++ Qry),
    io:format("Qry1: ~p~n", [Qry1]),
    Res =
        try
            Mod = xqerl_code_server:compile(filename:join(__BaseDir, "MapTest-057.xq"), Qry1),
            xqerl:run(Mod, Opts)
        of
            D -> D
        catch
            _:E -> E
        end,
    Out =
        case xqerl_test:assert_deep_eq(Res, "'c'") of
            true -> {comment, "Deep equal"};
            {false, F} -> F
        end,
    case Out of
        {comment, C} -> {comment, C};
        Err -> ct:fail(Err)
    end.
