VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/lib/gems/2.5.0/gems/eventmachine-le-1.1.7/tests/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/lib/gems/2.5.0/gems/eventmachine-le-1.1.7/tests/test_iterator.rb
require 'em_test_helper'

class TestEnumerable
  include Enumerable

  def each
    while (num = rand(20)) != 10 do
      yield num
    end
  end
end

class TestEnumerable2
  include Enumerable

  def each
    arr = ('a'..'g').to_a
    while it = arr.shift do
      yield it
    end
  end
end

class TestEnumerable3
  include Enumerable

  def each
    ('a'..'f').each.with_index do |ltr, index|
      break if ltr == 'e'
      eval("@#{ltr} = #{index}")
      yield ltr
    end
  end
end

class TestIterator < Test::Unit::TestCase

  def test_iterator_with_array
    assert_nothing_raised do
      EM.run {
        after = proc{ EM.stop }
        EM::Iterator.new(0..10, 10).each(nil, after){ |num,iter| iter.next }
      }
    end
  end

  def test_iterator_with_array_with_result
    nums = []
    EM.run {
      after = proc{ EM.stop }
      EM::Iterator.new((0..10)).each(nil, after){ |num,iter|
        nums << num
        iter.next
      }
    }
    res = (0..10).to_a
    assert_equal res, nums
  end

  def test_iterator_with_enumerable
    assert_nothing_raised do
      EM.run {
        en = TestEnumerable.new
        after = proc{ EM.stop }
        EM::Iterator.new(en, 10).each(nil, after){ |num, iter| iter.next }
      }
    end
  end

  def test_iterator_with_enumerable_with_result
    letters = []
    EM.run {
      en = TestEnumerable2.new
      after = proc{ EM.stop }
      EM::Iterator.new(en, 10).each(nil, after){ |ltr,iter|
        letters << ltr
        iter.next
      }
    }
    res = ('a'..'g').to_a
    assert_equal res, letters
  end

  def test_iterator_with_array_with_nils
    nums = []
    EM.run {
      after = proc{ EM.stop }
      EM::Iterator.new(["Hello", nil, "World", nil]).each(nil, after){ |num,iter|
        nums << num
        iter.next
      }
    }
    res = "Hello World"
    assert_equal res, nums.compact.join(" ")
  end

  def test_iterator_for_lazyness
    enumerable = TestEnumerable3.new
    enumerator = enumerable.to_enum
    EM.run {
      after = proc{ EM.stop }
      EM::Iterator.new(enumerator).each(nil, after){ |num,iter|
        iter.next
      }
    }
    assert_equal 0, enumerable.instance_variable_get(:@a)
    assert_equal 3, enumerable.instance_variable_get(:@d)
    assert_equal false, enumerable.instance_variable_defined?(:@e)
  end
end

VaKeR 2022