Class: Evilution::Equivalent::Heuristic::ArithmeticIdentity

Inherits:
Object
  • Object
show all
Defined in:
lib/evilution/equivalent/heuristic/arithmetic_identity.rb

Constant Summary collapse

ADDITIVE_IDENTITY =

Patterns where the original expression is an arithmetic identity operation. “x + 0” is identity (equals x), so mutating the 0 to something else means the original was a no-op — if the test doesn’t catch it, it’s likely equivalent.

/[\w)\].]+\s*[+-]\s*0\b|\b0\s*\+\s*[\w(\[]/
MULTIPLICATIVE_IDENTITY =
%r{[\w)\].]+\s*[*/]\s*1\b|\b1\s*\*\s*[\w(\[]}
EXPONENT_IDENTITY =
/[\w)\].]+\s*\*\*\s*1\b/

Instance Method Summary collapse

Instance Method Details

#match?(mutation) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/evilution/equivalent/heuristic/arithmetic_identity.rb', line 13

def match?(mutation)
  return false unless mutation.operator_name == "integer_literal"

  removed = diff_line(mutation.diff, "- ")
  return false unless removed

  content = removed.sub(/^- /, "")
  content.match?(ADDITIVE_IDENTITY) ||
    content.match?(MULTIPLICATIVE_IDENTITY) ||
    content.match?(EXPONENT_IDENTITY)
end