<commit_before>-- Copyright (c) 2009 Incremental IP Limited
-- see license.txt for license information

local series = require("test.series")
local add = require("rima.operators.add")
local object = require("rima.object")
local expression = require("rima.expression")
require("rima.types.number_t")
require("rima.public")
local rima = rima

module(...)

-- Tests -----------------------------------------------------------------------

function test(show_passes)
  local T = series:new(_M, show_passes)

  T:test(object.isa(add:new(), add), "isa(add, add:new())")
  T:check_equal(object.type(add:new()), "add", "type(add:new()) == 'add'")

--  T:expect_ok(function() add:check({}) end)
--  T:expect_ok(function() add:check({{1, 2}}) end) 

  T:check_equal(add:dump({{1, 1}}), "+(1*number(1))")
  T:check_equal(add:_tostring({{1, 1}}), "1")
  T:check_equal(add:dump({{1, 2}, {3, 4}}), "+(1*number(2), 3*number(4))")
  T:check_equal(add:_tostring({{1, 2}, {3, 4}}), "2 + 12")
  T:check_equal(add:_tostring({{-1, 2}, {3, 4}}), "-2 + 12")
  T:check_equal(add:_tostring({{-1, 2}, {-3, 4}}), "-2 - 12")

  local S = rima.scope.new()
  T:check_equal(add:eval(S, {{1, 2}}), 2)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}}), 14)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}, {5, 6}}), 44)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}, {-5, 6}}), -16)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}, {5, -6}}), -16)

  local a, b = rima.R"a,b"
  rima.scope.set(S, { a = 5, b = rima.positive() })
  T:check_equal(add:dump({{1, a}}), "+(1*ref(a))")
  T:check_equal(add:eval(S, {{1, a}}), 5)
  T:check_equal(add:eval(S, {{1, a}, {2, a}}), 15)

  T:check_equal(2 + (3 + b), "2 + 3 + b")
  T:check_equal(2 - (3 + b), "2 - (3 + b)")

  T:check_equal(rima.E(b - b, S), 0)
  T:check_equal(rima.E(b + b, S), "2*b")
  T:check_equal(rima.E(2 + (3 + b), S), "5 + b")
  T:check_equal(rima.E(2 - (3 + b), S), "-1 - b")

  T:check_equal(rima.E(2 + (3 + a), S), 10)
  T:check_equal(rima.E(2 - (3 + a), S), -6)
  T:check_equal(rima.E(-a, S), -5)
  T:check_equal(rima.E(2 + (3 + b), S), "5 + b")

  T:check_equal(expression.dump(add:eval(S, {{2, S.b}})), "+(2*ref(b))")
  T:check_equal(expression.dump(add:eval(S, {{1, S.b}})), "ref(b)", "checking we simplify identity")

  -- Tests including mul are in rima.expression
<commit_msg>rima: full coverage for the add tests<commit_after>-- Copyright (c) 2009 Incremental IP Limited
-- see license.txt for license information

local series = require("test.series")
local add = require("rima.operators.add")
local object = require("rima.object")
local expression = require("rima.expression")
require("rima.types.number_t")
require("rima.public")
local rima = rima

module(...)

-- Tests -----------------------------------------------------------------------

function test(show_passes)
  local T = series:new(_M, show_passes)

  T:test(object.isa(add:new(), add), "isa(add, add:new())")
  T:check_equal(object.type(add:new()), "add", "type(add:new()) == 'add'")

--  T:expect_ok(function() add:check({}) end)
--  T:expect_ok(function() add:check({{1, 2}}) end) 

  T:check_equal(add:dump({{1, 1}}), "+(1*number(1))")
  T:check_equal(add:_tostring({{1, 1}}), "1")
  T:check_equal(add:dump({{1, 2}, {3, 4}}), "+(1*number(2), 3*number(4))")
  T:check_equal(add:_tostring({{1, 2}, {3, 4}}), "2 + 12")
  T:check_equal(add:_tostring({{-1, 2}, {3, 4}}), "-2 + 12")
  T:check_equal(add:_tostring({{-1, 2}, {-3, 4}}), "-2 - 12")

  local S = rima.scope.new()
  T:check_equal(add:eval(S, {{1, 2}}), 2)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}}), 14)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}, {5, 6}}), 44)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}, {-5, 6}}), -16)
  T:check_equal(add:eval(S, {{1, 2}, {3, 4}, {5, -6}}), -16)

  local a, b, c = rima.R"a,b,c"
  rima.scope.set(S, { a = 5, b = rima.positive() })
  T:check_equal(add:dump({{1, a}}), "+(1*ref(a))")
  T:check_equal(add:eval(S, {{1, a}}), 5)
  T:check_equal(add:eval(S, {{1, a}, {2, a}}), 15)

  T:check_equal(2 + (3 + b), "2 + 3 + b")
  T:check_equal(2 - (3 + b), "2 - (3 + b)")

  T:check_equal(rima.E(b - b, S), 0)
  T:check_equal(rima.E(b + b, S), "2*b")
  T:check_equal(rima.E(2 + (3 + b), S), "5 + b")
  T:check_equal(rima.E(2 - (3 + b), S), "-1 - b")

  T:check_equal(rima.E(2 + (3 + a), S), 10)
  T:check_equal(rima.E(2 - (3 + a), S), -6)
  T:check_equal(rima.E(-a, S), -5)
  T:check_equal(rima.E(2 + (3 + b), S), "5 + b")

  T:check_equal(expression.dump(rima.E(2 + 3*b, S)), "+(1*number(2), 3*ref(b))")
  T:check_equal(expression.dump(rima.E(2 + b*c, S)), "+(1*number(2), 1**(ref(b)^1, ref(c)^1))")
  T:check_equal(expression.dump(rima.E(2 + 5*b*c, S)), "+(1*number(2), 5**(ref(b)^1, ref(c)^1))")

  T:check_equal(expression.dump(add:eval(S, {{2, S.b}})), "+(2*ref(b))")
  T:check_equal(expression.dump(add:eval(S, {{1, S.b}})), "ref(b)", "checking we simplify identity")

  -- Tests including mul are in rima.expression

{'fraction_non_alphanumeric': 0.29553334693045075, 'fraction_numerical': 0.05017336324699164, 'mean_word_length': 3.9585439838220426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7457909', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2184, 'n_words': 444}