<commit_before>/*
 * Copyright 2014–2016 SlamData Inc.
 *
 * 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.
 */

package quasar.std

import quasar.Func
import quasar.Predef._
import quasar.TypeArbitrary

import matryoshka.Fix
import org.scalacheck.Arbitrary
import org.threeten.bp.{Instant, Duration}
import scalaz.ValidationNel
import scalaz.Validation.FlatMap._
import shapeless._

class MathSpec extends quasar.Qspec with TypeArbitrary {
  import MathLib._
  import quasar.Type
  import quasar.Type.Const
  import quasar.Type.Obj
  import quasar.Data._
  import quasar.SemanticError
  import quasar.LogicalPlan, LogicalPlan._

  "MathLib" should {
    "type simple add with ints" in {
      val expr = Add.tpe(Func.Input2(Type.Int, Type.Int))
      expr should beSuccessful(Type.Int)
    }

    "type simple add with decs" in {
      val expr = Add.tpe(Func.Input2(Type.Dec, Type.Dec))
      expr should beSuccessful(Type.Dec)
    }

    "type simple add with promotion" in {
      val expr = Add.tpe(Func.Input2(Type.Int, Type.Dec))
      expr should beSuccessful(Type.Dec)
    }

    "type simple add with zero" in {
      val expr = Add.tpe(Func.Input2(Type.Numeric, TZero()))
      expr should beSuccessful(Type.Numeric)
    }

    "fold simple add with int constants" in {
      val expr = Add.tpe(Func.Input2(TOne(), Const(Int(2))))
      expr should beSuccessful(Const(Int(3)))
    }

    "fold simple add with decimal constants" in {
      val expr = Add.tpe(Func.Input2(Const(Dec(1.0)), Const(Dec(2.0))))
      expr should beSuccessful(Const(Dec(3)))
    }

    "fold simple add with promotion" in {
      val expr = Add.tpe(Func.Input2(TOne(), Const(Dec(2.0))))
      expr should beSuccessful(Const(Dec(3)))
    }

    "simplify add with zero" in {
      Add.simplify(Add(LogicalPlan.Constant(Int(0)), Free('x))) should
        beSome(FreeF[Fix[LogicalPlan]]('x))
    }

    "simplify add with Dec zero" in {
      Add.simplify(Add(Free('x), LogicalPlan.Constant(Dec(0.0)))) should
        beSome(FreeF[Fix[LogicalPlan]]('x))
    }

    "eliminate multiply by dec zero (on the right)" >> prop { (c : Const) =>
      val expr = Multiply.tpe(Func.Input2(c, Const(Dec(0.0))))
      expr should beSuccessful(TZero())
    }

    "eliminate multiply by zero (on the left)" >> prop { (c : Const) =>
      val expr = Multiply.tpe(Func.Input2(TZero(), c))
      expr should beSuccessful(TZero())
    }

    "fold simple division" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(6)), Const(Int(3))))
      expr should beSuccessful(Const(Int(2)))
    }

    "fold truncating division" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(5)), Const(Int(2))))
      expr should beSuccessful(Const(Int(2)))
    }

    "fold simple division (dec)" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(6)), Const(Dec(3.0))))
      expr should beSuccessful(Const(Dec(2.0)))
    }

    "fold division (dec)" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(5)), Const(Dec(2))))
      expr should beSuccessful(Const(Dec(2.5)))
    }<commit_msg>update MathLib test for non-truncating division<commit_after>/*
 * Copyright 2014–2016 SlamData Inc.
 *
 * 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.
 */

package quasar.std

import quasar.Func
import quasar.Predef._
import quasar.TypeArbitrary

import matryoshka.Fix
import org.scalacheck.Arbitrary
import org.threeten.bp.{Instant, Duration}
import scalaz.ValidationNel
import scalaz.Validation.FlatMap._
import shapeless._

class MathSpec extends quasar.Qspec with TypeArbitrary {
  import MathLib._
  import quasar.Type
  import quasar.Type.Const
  import quasar.Type.Obj
  import quasar.Data._
  import quasar.SemanticError
  import quasar.LogicalPlan, LogicalPlan._

  "MathLib" should {
    "type simple add with ints" in {
      val expr = Add.tpe(Func.Input2(Type.Int, Type.Int))
      expr should beSuccessful(Type.Int)
    }

    "type simple add with decs" in {
      val expr = Add.tpe(Func.Input2(Type.Dec, Type.Dec))
      expr should beSuccessful(Type.Dec)
    }

    "type simple add with promotion" in {
      val expr = Add.tpe(Func.Input2(Type.Int, Type.Dec))
      expr should beSuccessful(Type.Dec)
    }

    "type simple add with zero" in {
      val expr = Add.tpe(Func.Input2(Type.Numeric, TZero()))
      expr should beSuccessful(Type.Numeric)
    }

    "fold simple add with int constants" in {
      val expr = Add.tpe(Func.Input2(TOne(), Const(Int(2))))
      expr should beSuccessful(Const(Int(3)))
    }

    "fold simple add with decimal constants" in {
      val expr = Add.tpe(Func.Input2(Const(Dec(1.0)), Const(Dec(2.0))))
      expr should beSuccessful(Const(Dec(3)))
    }

    "fold simple add with promotion" in {
      val expr = Add.tpe(Func.Input2(TOne(), Const(Dec(2.0))))
      expr should beSuccessful(Const(Dec(3)))
    }

    "simplify add with zero" in {
      Add.simplify(Add(LogicalPlan.Constant(Int(0)), Free('x))) should
        beSome(FreeF[Fix[LogicalPlan]]('x))
    }

    "simplify add with Dec zero" in {
      Add.simplify(Add(Free('x), LogicalPlan.Constant(Dec(0.0)))) should
        beSome(FreeF[Fix[LogicalPlan]]('x))
    }

    "eliminate multiply by dec zero (on the right)" >> prop { (c : Const) =>
      val expr = Multiply.tpe(Func.Input2(c, Const(Dec(0.0))))
      expr should beSuccessful(TZero())
    }

    "eliminate multiply by zero (on the left)" >> prop { (c : Const) =>
      val expr = Multiply.tpe(Func.Input2(TZero(), c))
      expr should beSuccessful(TZero())
    }

    "fold simple division" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(6)), Const(Int(3))))
      expr should beSuccessful(Const(Int(2)))
    }

    "fold non-truncating division" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(5)), Const(Int(2))))
      expr should beSuccessful(Const(Dec(2.5)))
    }

    "fold simple division (dec)" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(6)), Const(Dec(3.0))))
      expr should beSuccessful(Const(Dec(2.0)))
    }

    "fold division (dec)" in {
      val expr = Divide.tpe(Func.Input2(Const(Int(5)), Const(Dec(2))))
      expr should beSuccessful(Const(Dec(2.5)))
    }
{'fraction_non_alphanumeric': 0.12636135157777156, 'fraction_numerical': 0.015498464116168668, 'mean_word_length': 3.2815301852958756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6430217', 'n_tokens_mistral': 2420, 'n_tokens_neox': 2334, 'n_words': 643}