File: unique_call_path_test.rb

package info (click to toggle)
ruby-prof 0.17.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,656 kB
  • sloc: ruby: 5,043; ansic: 2,175; makefile: 6
file content (202 lines) | stat: -rwxr-xr-x 4,446 bytes parent folder | download | duplicates (2)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)

class UniqueCallPath
  def method_a(i)
    if i==1
      method_b
    else
      method_c
    end
  end

  def method_b
    method_c
  end

  def method_c
  end

  def method_k(i)
    method_a(i)
  end
end


# --  Tests ----
class UniqueCallPathTest < TestCase
  def test_root_method
    unique_call_path = UniqueCallPath.new

    result = RubyProf.profile do
      unique_call_path.method_a(1)
    end

    root_methods = Array.new
    result.threads.each do |thread|
      thread.methods.each do | m |
        if m.root?
          root_methods.push(m)
        end
      end
    end

    assert_equal(1, root_methods.length)
    assert_equal("UniqueCallPathTest#test_root_method", root_methods[0].full_name)
  end

  def test_root_children
    unique_call_path = UniqueCallPath.new

    result = RubyProf.profile do
      unique_call_path.method_a(1)
      unique_call_path.method_k(2)
    end

    root_methods = Array.new
    result.threads.each do |thread|
      thread.methods.each do | m |
        if m.root?
          root_methods.push(m)
        end
      end
    end

    assert_equal(1, root_methods.length)

    root_children = Array.new
    root_methods[0].children.each do | c |
      if c.parent.target.eql?(root_methods[0])
        root_children.push(c)
      end
    end

    children = root_children.sort do |c1, c2|
      c1.target.full_name <=> c2.target.full_name
    end

    assert_equal(2, children.length)
    assert_equal("UniqueCallPath#method_a", children[0].target.full_name)
    assert_equal("UniqueCallPath#method_k", children[1].target.full_name)
  end

  def test_children_of
    unique_call_path = UniqueCallPath.new

    result = RubyProf.profile do
      unique_call_path.method_a(1)
      unique_call_path.method_k(2)
    end

    root_methods = Array.new
    result.threads.each do |thread|
      thread.methods.each do | m |
        if m.root?
          root_methods.push(m)
        end
      end
    end

    assert_equal(1, root_methods.length)
    method = root_methods[0]
    assert_equal('UniqueCallPathTest#test_children_of', method.full_name)

    call_info_a = nil
    root_methods[0].children.each do | c |
      if c.target.full_name == "UniqueCallPath#method_a"
        call_info_a = c
        break
      end
    end

    assert !call_info_a.nil?

    children_of_a = Array.new

    call_info_a.children.each do | c |
      if c.parent.eql?(call_info_a)
        children_of_a.push(c)
      end
    end

    assert_equal(2, call_info_a.target.children.length)

    children_of_a = children_of_a.sort do |c1, c2|
      c1.target.full_name <=> c2.target.full_name
    end

    assert_equal(1, children_of_a.length)
    assert_equal("UniqueCallPath#method_b", children_of_a[0].target.full_name)
  end

  def test_id2ref
    unique_call_path = UniqueCallPath.new

    result = RubyProf.profile do
      unique_call_path.method_a(1)
    end

    root_methods = Array.new
    result.threads.each do |thread|
      thread.methods.each do | m |
        if m.root?
          root_methods.push(m)
        end
      end
    end

    child = root_methods[0].children[0]

    refute_equal(0, child.object_id)
    #assert_equal(RubyProf::CallInfo.id2ref(child.id).target.full_name, child.target.full_name)
  end

  def test_unique_path
    unique_call_path = UniqueCallPath.new

    result = RubyProf.profile do
      unique_call_path.method_a(1)
      unique_call_path.method_k(1)
    end

    root_methods = Array.new
    result.threads.each do |thread|
      thread.methods.each do | m |
        if m.root?
          root_methods.push(m)
        end
      end
    end

    assert_equal(1, root_methods.length)

    call_info_a = nil
    root_methods[0].children.each do | c |
      if c.target.full_name == "UniqueCallPath#method_a"
        call_info_a = c
        break
      end
    end

    assert !call_info_a.nil?

    children_of_a = Array.new
    call_info_a.children.each do |c|
      if c.parent.eql?(call_info_a)
        children_of_a.push(c)
      end
    end

    assert_equal(2, call_info_a.target.children.length)

    children_of_a = children_of_a.sort do |c1, c2|
      c1.target.full_name <=> c2.target.full_name
    end

    assert_equal(1, children_of_a.length)
    assert_equal(1, children_of_a[0].called)
    assert_equal("UniqueCallPath#method_b", children_of_a[0].target.full_name)
  end
end