# frozen_string_literal: false require 'test/unit' class TestComparable < Test::Unit::TestCase def setup @o = Object.new @o.extend(Comparable) end def cmp(b) class << @o; self; end.class_eval { undef :<=> define_method(:<=>, b) } end def test_equal cmp->(x) do 0; end assert_equal(true, @o == nil) cmp->(x) do 1; end assert_equal(false, @o == nil) cmp->(x) do nil; end assert_equal(false, @o == nil) cmp->(x) do raise NotImplementedError, "Not a RuntimeError" end assert_raise(NotImplementedError) { @o == nil } bug7688 = 'Comparable#== should not silently rescue' \ 'any Exception [ruby-core:51389] [Bug #7688]' cmp->(x) do raise StandardError end assert_raise(StandardError, bug7688) { @o == nil } cmp->(x) do "bad value"; end assert_raise(ArgumentError, bug7688) { @o == nil } end def test_gt cmp->(x) do 1; end assert_equal(true, @o > nil) cmp->(x) do 0; end assert_equal(false, @o > nil) cmp->(x) do -1; end assert_equal(false, @o > nil) end def test_ge cmp->(x) do 1; end assert_equal(true, @o >= nil) cmp->(x) do 0; end assert_equal(true, @o >= nil) cmp->(x) do -1; end assert_equal(false, @o >= nil) end def test_lt cmp->(x) do 1; end assert_equal(false, @o < nil) cmp->(x) do 0; end assert_equal(false, @o < nil) cmp->(x) do -1; end assert_equal(true, @o < nil) end def test_le cmp->(x) do 1; end assert_equal(false, @o <= nil) cmp->(x) do 0; end assert_equal(true, @o <= nil) cmp->(x) do -1; end assert_equal(true, @o <= nil) end def test_between cmp->(x) do 0 <=> x end assert_equal(false, @o.between?(1, 2)) assert_equal(false, @o.between?(-2, -1)) assert_equal(true, @o.between?(-1, 1)) assert_equal(true, @o.between?(0, 0)) end def test_clamp cmp->(x) do 0 <=> x end assert_equal(1, @o.clamp(1, 2)) assert_equal(-1, @o.clamp(-2, -1)) assert_equal(@o, @o.clamp(-1, 3)) assert_equal(1, @o.clamp(1, 1)) assert_equal(@o, @o.clamp(0, 0)) assert_raise_with_message(ArgumentError, 'min argument must be smaller than max argument') { @o.clamp(2, 1) } end def test_clamp_with_range cmp->(x) do 0 <=> x end assert_equal(1, @o.clamp(1..2)) assert_equal(-1, @o.clamp(-2..-1)) assert_equal(@o, @o.clamp(-1..3)) assert_equal(1, @o.clamp(1..1)) assert_equal(@o, @o.clamp(0..0)) assert_equal(1, @o.clamp(1..)) assert_equal(1, @o.clamp(1...)) assert_equal(@o, @o.clamp(0..)) assert_equal(@o, @o.clamp(0...)) assert_equal(@o, @o.clamp(..2)) assert_equal(-1, @o.clamp(-2..-1)) assert_equal(@o, @o.clamp(-2..0)) assert_equal(@o, @o.clamp(-2..)) assert_equal(@o, @o.clamp(-2...)) exc = [ArgumentError, 'cannot clamp with an exclusive range'] assert_raise_with_message(*exc) {@o.clamp(1...2)} assert_raise_with_message(*exc) {@o.clamp(0...2)} assert_raise_with_message(*exc) {@o.clamp(-1...0)} assert_raise_with_message(*exc) {@o.clamp(...2)} assert_raise_with_message(ArgumentError, 'min argument must be smaller than max argument') { @o.clamp(2..1) } end def test_err assert_raise(ArgumentError) { 1.0 < nil } assert_raise(ArgumentError) { 1.0 < Object.new } e = EnvUtil.labeled_class("E\u{30a8 30e9 30fc}") assert_raise_with_message(ArgumentError, /E\u{30a8 30e9 30fc}/) { 1.0 < e.new } end def test_inversed_compare bug7870 = '[ruby-core:52305] [Bug #7870]' assert_nothing_raised(SystemStackError, bug7870) { assert_nil(Time.new <=> "") } end def test_no_cmp bug9003 = '[ruby-core:57736] [Bug #9003]' assert_nothing_raised(SystemStackError, bug9003) { @o <=> @o.dup } end end 858136ba'>add #include guard hack卜部昌平 2019-12-28Add VM insns counter like debug_counter (#2789)Takashi Kokubun 2019-09-20Fixed format specifiersNobuyoshi Nakada 2019-09-19Avoid unneeded casts in INSN_ENTRY_SIGTakashi Kokubun 2019-09-19Drop PREFETCH macro unused since 6b534134a7Takashi Kokubun 2019-09-07Avoid defining DISPATCH_ARCH_DEPEND_WAY macroTakashi Kokubun 2019-03-08use __GNUC__ instead of __GCC__.tadd 2018-09-21fix typo.ko1 2018-03-04mjit_compile.c: use local variables for stackk0kubun 2018-03-03vm.c: add mjit_enable_p flagk0kubun 2018-02-04mjit_compile.c: merge initial JIT compilerk0kubun 2018-01-12delete tool/instruction.rb (2nd try)shyouhei 2018-01-10merge revisions 61753:61750 61747:61740 61737:61728shyouhei 2018-01-09delete tool/instruction.rbshyouhei 2018-01-02label as lvalue is a GCCismshyouhei 2017-12-11vm_exec.h: introduce macros for abstarction.ko1 2017-11-14remove `trace` instruction. [Feature #14104]ko1 2017-10-29`th` -> `ec` for VM dump related functions.ko1 2017-10-27catch up recent changes for call threaded code VM.ko1 2017-10-27`ec` -> `th`ko1 2017-10-26Use rb_execution_context_t instead of rb_thread_tko1 2017-08-10rename rb_execution_context_t::stack(_size) to vm_stack(_size).ko1 2017-06-28move fields to ec.ko1 2017-05-09rb_execution_context_t: move stack, stack_size and cfp from rb_thread_tnormal 2013-12-18* vm_exec.h (VM_DEBUG_STACKOVERFLOW): added.ko1 2013-11-29 * vm_dump.c (rb_vmdebug_debug_print_pre): Bugfix. Get PC directly.tarui 2013-11-18vm_core.h: extract VM_STACK_OVERFLOWED_Pnobu 2013-07-26vm_exec.h: fix CHECK_VM_STACK_OVERFLOW_FOR_INSNnobu 2013-07-26* vm_exec.h, tool/instruction.rb: not an error, but a BUG if stackko1 2013-04-22fix minor code comment typostmm1 2013-03-18* vm_exec.h (END_INSN): revert r39517 because the segv seems fixed bynaruse 2013-02-27* vm_exec.h (END_INSN): llvm-gcc may optimize out reg_cfp and causenaruse 2013-02-10vm_exec.h: fix typonobu 2013-02-10* vm_exec.h (DISPATCH_ARCH_DEPEND_WAY): use __asm__ __vilatile__kosaki 2012-12-29adjust stylenobu 2012-11-15* vm_exec.h (GENTRY): GENTRY should be pointer size.ko1 2012-10-04* insns.def (getlocal, setlocal): remove old getlocal/setlocalko1 2012-10-04* vm.c (VM_COLLECT_USAGE_DETAILS): make new VM usage analysisko1 2012-07-25Suppress warnings.naruse 2012-06-11* vm_core.h: remove lfp (local frame pointer) and renameko1 2011-01-23* vm_exec.h: parenthesize macro arguments.akr 2010-10-12* configure.in (RUBY_CHECK_PRINTF_PREFIX): check for printf formatnobu 2010-07-27* class.c, compile.c, dir.c, file.c, iseq.c, parse.y, random.c:naruse 2010-01-05removes the dtrace support. reverts r26239, r26238 and r26235.yugui 2010-01-03* trace.h: new file. wraps tracing mechanisms.yugui 2009-01-19* vm_dump.c: add a prefix "rb_vmdebug_" toko1 2008-11-19* vm_exec.h (RUBY_VM_EXEC_H): fixed include guard.nobu 2008-09-23* common.mk: clean upko1