<commit_before>%% See LICENSE for licensing information.
%% -*- coding: utf-8 -*-
-module(wheredim_tests).

%% tea_tests: tests for the interpreter.

-include_lib("eunit/include/eunit.hrl").


%% API tests.

e1_test_ () ->
    {ok, T} = tea:string(
        "X
        where
            dim t <- 0
            var X = #.t
        end"),
    TimeD = {[0],"t"},
    D = [TimeD],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({0,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

wherevar_only_test_ () ->
    {ok, T} = tea:string(
        "X
        where
            var X = 46
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({46,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

wheredim_only_test_ () ->
    {ok, T} = tea:string(
        "#.t
        where
            dim t <- 58
        end"),
    TimeD = {[0],"t"},
    D = [TimeD],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({58,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e2_test_ () ->
    {ok, T} = tea:string(
        "#.t + #.s
        where
            dim t <- 1
        end"),
    TimeD = {[0],"t"},
    SpaceD = {[0],"s"},
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({[TimeD,SpaceD],_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e3_test_ () ->
    {ok, T} = tea:string(
        "#.t + #.s
        where
            dim t <- 2
            dim s <- 3
        end"),
    TimeD = {[0],"t"},
    SpaceD = {[0],"s"},
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({[TimeD,SpaceD],_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e4_test_ () ->
    {ok, T} = tea:string(
        "X
        where
            var X = #.t + #.s
            dim t <- 2
            dim s <- 3
        end"),
    TimeD = {[0],"t"},
    SpaceD = {[0],"s"},
    D = [TimeD,SpaceD],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({5,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e5_test_ () ->
    %% Sequential, multi-dimensional wheredim clause
    {ok, T} = tea:string(
        "N0
        where
            var N0 = if #.t == 0
                     then N1
                     else N0 @ [t <- #.t - 1]
                     fi
            var N1 = if #.s == 0
                     then 1
                     else N1 @ [s <- #.s - 1] * 2
                     fi
            dim t <- 10
            dim s <- 10
        end"),
    TimeD = {[0],"t"},
    SpaceD = {[0],"s"},
    D = [TimeD,SpaceD],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({1024,_}, % Not sure about 1024 here.
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e6_test_ () ->
    %% Parallel, one-dimensional (tournament) wheredim clause (introducing parallelism)
    {ok, T} = tea:string(
        "// Tournament in 1 dimension
        A
        where
            dim t <- 2
            dim s <- 0

            // Compute A across space
            var A =
                if #.t <= 0 then
                    B
                else
                    (A @ [s <- #.s * 2] + A @ [s <- #.s * 2 + 1]) @ [t <- #.t - 1]
                fi

            // Ensure spatial values are between 1 and 1024
            var B =
               if #.s >= 1 and #.s <= 1024 then
                    #.s
                else
                    1
                fi
        end"),
    TimeD = {[0],"t"},
    SpaceD = {[0],"s"},
    D = [TimeD,SpaceD],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({7, _},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e7_test_ () ->
    %% Parallel, two-dimensional (tournament)
    {ok, T} = tea:string(
        "Y1 @ [x <- 0]
        where
            dim t <- 2
            var Y1 = if #.t <= 0 then X1
                     else ( Y1 @ [x <- #.x * 2,     y <- #.y * 2]
                          + Y1 @ [x <- #.x * 2 + 1, y <- #.y * 2]
                          + Y1 @ [x <- #.x * 2,     y <- #.y * 2 + 1]
                          + Y1 @ [x <- #.x * 2 + 1, y <- #.y * 2 + 1]
                          ) @ [t <- #.t - 1]
                     fi
            var X1 = if     #.x >= 1 and #.x <= 1024
                        and #.y >= 1 and #.y <= 1024
                     then #.x
                     else 1
                     fi
            dim x <- 0
            dim y <- 0
        end"),
    TimeD = {[0],"t"},
    D = [TimeD],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({25, _},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e8_test () ->
    %% Parallel, multi-dimensional matrix multiplication (the one you're all waiting for ;))
    ok.

%% Internals

%% End of Module.<commit_msg>Fix in tests the usage of non-unique dimension identifiers in wheredim<commit_after>%% See LICENSE for licensing information.
%% -*- coding: utf-8 -*-
-module(wheredim_tests).

%% tea_tests: tests for the interpreter.

-include_lib("eunit/include/eunit.hrl").


%% API tests.

e1_test_ () ->
    {ok, T} = tea:string(
        "X
        where
            dim t <- 0
            var X = #.t
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({0,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

wherevar_only_test_ () ->
    {ok, T} = tea:string(
        "X
        where
            var X = 46
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({46,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

wheredim_only_test_ () ->
    {ok, T} = tea:string(
        "#.t
        where
            dim t <- 58
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({58,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e2_test_ () ->
    {ok, T} = tea:string(
        "#.t + #.s
        where
            dim t <- 1
        end"),
    SpaceD = {[0],"s"},
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({[SpaceD],_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e3_test_ () ->
    {ok, T} = tea:string(
        "#.t + #.s
        where
            dim t <- 2
            dim s <- 3
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({5,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e4_test_ () ->
    {ok, T} = tea:string(
        "X
        where
            var X = #.t + #.s
            dim t <- 2
            dim s <- 3
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({5,_},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e5_test_ () ->
    %% Sequential, multi-dimensional wheredim clause
    {ok, T} = tea:string(
        "N0
        where
            var N0 = if #.t == 0
                     then N1
                     else N0 @ [t <- #.t - 1]
                     fi
            var N1 = if #.s == 0
                     then 1
                     else N1 @ [s <- #.s - 1] * 2
                     fi
            dim t <- 10
            dim s <- 10
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({1024,_}, % Not sure about 1024 here.
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e6_test_ () ->
    %% Parallel, one-dimensional (tournament) wheredim clause (introducing parallelism)
    {ok, T} = tea:string(
        "// Tournament in 1 dimension
        A
        where
            dim t <- 2
            dim s <- 0

            // Compute A across space
            var A =
                if #.t <= 0 then
                    B
                else
                    (A @ [s <- #.s * 2] + A @ [s <- #.s * 2 + 1]) @ [t <- #.t - 1]
                fi

            // Ensure spatial values are between 1 and 1024
            var B =
               if #.s >= 1 and #.s <= 1024 then
                    #.s
                else
                    1
                fi
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({7, _},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e7_test_ () ->
    %% Parallel, two-dimensional (tournament)
    {ok, T} = tea:string(
        "Y1 @ [x <- 0]
        where
            dim t <- 2
            var Y1 = if #.t <= 0 then X1
                     else ( Y1 @ [x <- #.x * 2,     y <- #.y * 2]
                          + Y1 @ [x <- #.x * 2 + 1, y <- #.y * 2]
                          + Y1 @ [x <- #.x * 2,     y <- #.y * 2 + 1]
                          + Y1 @ [x <- #.x * 2 + 1, y <- #.y * 2 + 1]
                          ) @ [t <- #.t - 1]
                     fi
            var X1 = if     #.x >= 1 and #.x <= 1024
                        and #.y >= 1 and #.y <= 1024
                     then #.x
                     else 1
                     fi
            dim x <- 0
            dim y <- 0
        end"),
    D = [],
    {setup, fun () -> tcache:start_link(100) end,
            fun (_) -> tcache:stop() end,
    fun () ->
        ?assertMatch({25, _},
            tcore:eval(T, [],[],[], D, [0], 0))
    end}.

e8_test () ->
    %% Parallel, multi-dimensional matrix multiplication (the one you're all waiting for ;))
    ok.

%% Internals

%% End of Module.
{'fraction_non_alphanumeric': 0.18669755767931268, 'fraction_numerical': 0.030987547060527077, 'mean_word_length': 1.1290587751746815, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '887095', 'n_tokens_mistral': 3841, 'n_tokens_neox': 3511, 'n_words': 935}