File: test_LogicalExpression.rb

package info (click to toggle)
tj3 3.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,048 kB
  • sloc: ruby: 36,481; javascript: 1,113; sh: 19; makefile: 17
file content (121 lines) | stat: -rw-r--r-- 3,097 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = test_LogicalExpression.rb -- The TaskJuggler III Project Management Software
#
# Copyright (c) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
#               by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib') if __FILE__ == $0

require 'test/unit'

require 'taskjuggler/LogicalExpression'

class TaskJuggler

class TestLogicalExpression < Test::Unit::TestCase

  def setup
  end

  def teardown
  end

  def test_unaryOperations
    parameter = [
      [ true, '~', false ],
      [ false, '~', true ]
    ]

    parameter.each do |op, operator, result|
      exp = LogicalExpression.new(unaryOp(op, operator))
      assert_equal(result, exp.eval(nil),
                  "Operation #{operator} #{op} -> #{result} failed")
    end
  end

  def test_binaryOperations
    parameter = [
      [ 2, '<', 3, true ],
      [ 3, '<', 2, false ],
      [ 2, '<', 2, false ],
      [ 4, '>', 5, false ],
      [ 5, '>', 4, true ],
      [ 5, '>', 5, false ],
      [ 'a', '>', 'b', false ],
      [ 'b', '>', 'a', true ],
      [ 2, '<=', 3, true ],
      [ 3, '<=', 2, false ],
      [ 2, '<=', 2, true],
      [ 4, '>=', 5, false ],
      [ 5, '>=', 4, true ],
      [ 5, '>=', 5, true],
      [ 'A', '>=', 'B', false ],
      [ 'A', '>=', 'A', true ],
      [ 'B', '>=', 'B', true ],
      [ 6, '=', 5, false ],
      [ 6, '=', 6, true],
      [ 'c', '=', 'c', true ],
      [ 'x', '=', 'y', false ],
      [ '', '=', '', true ],
      [ '', '=', 'a', false ],
      [ true, '&', true, true ],
      [ true, '&', false, false ],
      [ false, '&', true, false ],
      [ false, '&', false, false ],
      [ 1, '&', 1, true ],
      [ 1, '&', 0, false ],
      [ 0, '&', 1, false ],
      [ 0, '&', 0, false ],
      [ true, '|', true, true ],
      [ true, '|', false, true ],
      [ false, '|', true, true ],
      [ false, '|', false, false ]
    ]

    parameter.each do |op1, operator, op2, result|
      exp = LogicalExpression.new(binaryOp(op1, operator, op2))
      assert_equal(result, exp.eval(nil),
                  "Operation #{op1} #{operator} #{op2} -> #{result} failed")
    end
  end

  def test_operationTrees
    op1 = binaryOp(2, '<', 4)
    op2 = binaryOp(3, '>', 6)
    exp = LogicalExpression.new(binaryOp(op1, '|', op2))
    assert_equal(true, exp.eval(nil),
                 "Operation #{exp} -> true failed")
  end

  def test_exceptions
    begin
      exp = LogicalExpression.new(binaryOp(false, '<', true))
      assert_raise TjException do
        exp.eval(nil)
      end
    rescue TjException
    end
  end

private

  def binaryOp(op1, operator, op2)
    LogicalOperation.new(LogicalOperation.new(op1), operator,
                         LogicalOperation.new(op2))
  end

  def unaryOp(op, operator)
    LogicalOperation.new(LogicalOperation.new(op), operator)
  end

end

end