inifinity recursion && test/unit

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default inifinity recursion && test/unit

    Without test/unit ruby finished work and said that stack level too deep.
    Is this correct that in this code ruby goes to infinity recursion?

    ruby 1.8.0 (2003-06-06) [i386-freebsd4.7]

    ---
    #!/usr/local/bin/ruby
    require 'test/unit'

    module Recur
    def self.new
    Recur.new
    end
    end

    class Tests < Test::Unit::TestCase
    def test_1
    Recur.new
    end
    end

    --
    with best regards,
    Andrey Kulinich
    IT Group
    Software developer
    phone/fax +380 (372) 58-43-10
    e-mail: [email]Andrey.Kulinich@itgrp.net[/email]
    [url]http://www.itgrp.net[/url]


    Andrey Kulinich Guest

  2. Similar Questions and Discussions

    1. Test::Unit non-auto-run test case?
      I'm getting a handle on the Test::Unit library, and the automatically-running test case example was extremely simple to get running, but now I want...
    2. Test::Unit -- multiple errors in test method ???
      Hi ! I have been writing some unit tests with Test::Unit. I've noted that when an assertion fails in a test method, the remaining assertions...
    3. Method test::unit::TestSuite#<<(test)
      Hi, I suggest to change the definition of this method slightly: current: # Adds the test to the suite. def <<(test) @tests << test end
    4. Test order in Test::Unit
      On Sun, Jul 06, 2003 at 02:05:57AM +0900, Nathaniel Talbott wrote: instance_eval is also extremely useful; it lets you get at instance variables...
    5. inifinite recursion && test/unit
      > On Tue, Jul 01, 2003 at 10:06:46PM +0900, Andrey Kulinich wrote: I'd guess so to--. Wouldn't this be a "halting problem" issue? =)
  3. #2

    Default Re: inifinity recursion && test/unit

    On Tue, Jul 01, 2003 at 05:34:06PM +0900, Andrey Kulinich wrote:
    > Without test/unit ruby finished work and said that stack level too deep.
    > Is this correct that in this code ruby goes to infinity recursion?
    >
    > ruby 1.8.0 (2003-06-06) [i386-freebsd4.7]
    >
    > ---
    > #!/usr/local/bin/ruby
    > require 'test/unit'
    >
    > module Recur
    > def self.new
    > Recur.new
    > end
    > end
    >
    > class Tests < Test::Unit::TestCase
    > def test_1
    > Recur.new
    > end
    > end
    Yep, that's definitely a nice piece of infinite recursion. What are you
    trying to achieve?

    module Recur
    def self.new ==> same as def Recur.new

    Regards,

    Brian.

    Brian Candler Guest

  4. #3

    Default Re: inifinity recursion && test/unit



    Brian Candler wrote:
    >>#!/usr/local/bin/ruby
    >>require 'test/unit'
    >>
    >>module Recur
    >> def self.new
    >> Recur.new
    >> end
    >>end
    >>
    >>class Tests < Test::Unit::TestCase
    >> def test_1
    >> Recur.new
    >> end
    >>end
    > Yep, that's definitely a nice piece of infinite recursion. What are you
    > trying to achieve?
    Originally that was an error. Code:
    -
    module Recur
    def self.new
    Recur.new
    end

    class Recur
    end
    end

    Recur.new
    -
    But after that I rename class Recur to Another and forgot to change in
    def self.new.
    Without test/unit ruby said that there is an infinite recursion. Is that
    a bug or a feature?
    > module Recur
    > def self.new ==> same as def Recur.new
    Almost. Depends on position of definition.

    --
    with best regards,
    Andrey Kulinich
    IT Group
    Software developer
    phone/fax +380 (372) 58-43-10
    e-mail: [email]Andrey.Kulinich@itgrp.net[/email]
    [url]http://www.itgrp.net[/url]





    Andrey Kulinich Guest

  5. #4

    Default Re: inifinity recursion && test/unit

    On Tue, Jul 01, 2003 at 08:38:42PM +0900, Andrey Kulinich wrote:
    > Originally that was an error. Code:
    > -
    > module Recur
    > def self.new
    > Recur.new
    > end
    >
    > class Recur
    > end
    > end
    >
    > Recur.new
    OK, so you have defined:
    - a module Recur
    - a class Recur::Recur
    - a module function Recur::new which calls Recur::Recur.new
    (it looks in its own namespace before trying enclosing namespaces,
    so Recur::Recur.new is used if it exists; if it doesn't then it will
    find Recur.new in the top-level namespace)

    So this is not recursive.
    > But after that I rename class Recur to Another and forgot to change in
    > def self.new.
    > Without test/unit ruby said that there is an infinite recursion. Is that
    > a bug or a feature?
    Neither, it's correct behaviour. If class Recur no longer exists, then the
    call to 'Recur.new' will refer to the function 'new' in module Recur, which
    is itself (hence infinitely recursive).

    Regards,

    Brian.

    Brian Candler Guest

  6. #5

    Default Re: inifinite recursion && test/unit

    Brian Candler wrote:
    >>But after that I rename class Recur to Another and forgot to change in
    >>def self.new.
    >>Without test/unit ruby said that there is an infinite recursion. Is that
    >>a bug or a feature?
    > Neither, it's correct behaviour. If class Recur no longer exists, then the
    > call to 'Recur.new' will refer to the function 'new' in module Recur, which
    > is itself (hence infinitely recursive).
    I was talking about test/unit.
    Why when I'm using it in such code ruby can't detect infinite loop? In
    my opinion this is test/unit's bug.

    --
    with best regards,
    Andrey Kulinich
    IT Group
    Software developer
    phone/fax +380 (372) 58-43-10
    e-mail: [email]Andrey.Kulinich@itgrp.net[/email]
    [url]http://www.itgrp.net[/url]



    Andrey Kulinich Guest

  7. #6

    Default Re: inifinite recursion && test/unit

    On Tue, Jul 01, 2003 at 10:06:46PM +0900, Andrey Kulinich wrote:
    > I was talking about test/unit.
    > Why when I'm using it in such code ruby can't detect infinite loop? In
    > my opinion this is test/unit's bug.
    I don't think Ruby really "detects" infinite loops; it simply runs out of
    memory and reports it.

    When I run your code from RubyTalk:74829 I get:

    $ ruby x.rb 2>&1 | head -20
    Loaded suite x
    Started
    ..
    Error!!!
    test_1(Tests):
    SystemStackError: stack level too deep
    x.rb:6:in `new'
    x.rb:6:in `new'
    x.rb:6:in `new'
    x.rb:6:in `new'
    x.rb:6:in `new'
    x.rb:6:in `new'
    ... etc

    Now if I strip out the test/unit stuff I get almost exactly the same error:

    $ cat x.rb
    #!/usr/local/bin/ruby
    module Recur
    def self.new
    Recur.new
    end
    end

    Recur.new

    $ ruby x.rb 2>&1
    x.rb:4:in `New': stack level too deep (SystemStackError)
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    ... 66668 levels...
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:4:in `New'
    from x.rb:8

    The only difference seems to be that the error report has this optimisation
    of "... 66668 levels..." in there (This is ruby-1.6.8, incidentally)

    test/unit traps exceptions, so that
    (a) it can count errors, and
    (b) it can display errors in the appropriate place (you might be running
    with a GTk testrunner, for example)

    So I wouldn't expect it to give *exactly* the same output as Ruby's default
    exception handler. It does seem pretty close to me though.

    If you don't think test/unit is doing the right thing, can you post the
    output of running your test program under test/unit, the output when run
    without test/unit, and how you think it should be changed?

    Regards,

    Brian.

    Brian Candler Guest

  8. #7

    Default Re: inifinite recursion && test/unit

    On Tue, 1 Jul 2003 22:06:46 +0900
    Andrey Kulinich <Andrey.Kulinich@itgrp.net> wrote:
    > > Neither, it's correct behaviour. If class Recur no longer exists, then the
    > > call to 'Recur.new' will refer to the function 'new' in module Recur,
    > > which is itself (hence infinitely recursive).
    >
    > I was talking about test/unit.
    > Why when I'm using it in such code ruby can't detect infinite loop? In
    > my opinion this is test/unit's bug.
    Huh? What do you expect Test::Unit to do? Given this input, with
    ruby-1.8.0-preview3 and whatever version of test/unit comes with that:

    require 'test/unit'

    def recursive()
    recursive()
    end

    class TC_recursive < Test::Unit::TestCase
    def test_recursive
    recursive()
    end
    end

    ....when run, will give this output:

    Loaded suite recur
    Started
    E
    Finished in 0.098239 seconds.

    1) Error!!!
    test_recursive(TC_recursive):
    SystemStackError: stack level too deep
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    <snipped several thousand lines of traceback>
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:6:in `recursive'
    recur.rb:11:in `test_recursive'
    recur.rb:10

    1 tests, 0 assertions, 0 failures, 1 errors

    What's the problem?

    Jason Creighton

    Jason Creighton Guest

  9. #8

    Default Re: inifinite recursion && test/unit

    Andrey Kulinich <Andrey.Kulinich@itgrp.net> writes:
    > I was talking about test/unit.
    > Why when I'm using it in such code ruby can't detect infinite loop? In
    > my opinion this is test/unit's bug.
    By definition, a program can't detect whether it'll exit or not
    without actually running itself. It's known as the Halting Problem,
    and it's a fundamental theorem of Computer Science.

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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