# Exhaustive tests for struct.c, sprintf.c and math.c

**URL:** <https://www.ruby-forum.com/t/exhaustive-tests-for-struct-c-sprintf-c-and-math-c/126826>\
**Category:** Development\
**Created:** [January 22, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/exhaustive-tests-for-struct-c-sprintf-c-and-math-c/126826 "2008-01-22T16:04:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 22, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/exhaustive-tests-for-struct-c-sprintf-c-and-math-c/126826/1 "2008-01-22T16:04:59Z")

</div>

e$B1sF#$H?=$7$^$9!#e(B

test\_struct.rb e$B$He(B test\_sprintf.rb e$B$He(B test\_math.rb  
e$B$K$b%F%9%H$rDI2Ce(B  
e$B$7$^$7$?!#e(Bgcov e$B$G$N%+%P%l%C%8$,0J2\<$N$h$&$K\>e$,$j$^$9!#e(B

struct.c : 55.87% -\> 94.60%  
sprintf.c : 70.45% -\> 97.50%  
math.c : 28.36% -\> 98.51%

e$B$3$l$b%3%\_%C%H$7$F$h$m$7$$$G$7$g$&$+!#$h$1$l$P$=$N$&$AB\>$G$b$d$m$&$He(B  
e$B;W$$$^$9!#e(B

# Index: test/ruby/test\_struct.rb

— test/ruby/test\_struct.rb (revision 15173)  
+++ test/ruby/test\_struct.rb (working copy)  
@@ -49,4 +49,162 @@  
}  
}  
end  
+

- def test\_inherit

- klass = Struct.new(:a)

- klass2 = Class.new(klass)

- o = klass2.new(1)

- assert\_equal(1, o.a)

- end

- 
- def test\_members

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_equal([:a], klass.members)

- assert\_equal([:a], o.members)

- end

- 
- def test\_ref

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_equal(1, o[:a])

- assert\_raise(NameError) { o[:b] }

- end

- 
- def test\_modify

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_raise(SecurityError) do

- 

```
 Thread.new do

```

- 

```
   $SAFE = 4

```

- 

```
   o.a = 2

```

- 

```
 end.value

```

- end

- end

- 
- def test\_set

- klass = Struct.new(:a)

- o = klass.new(1)

- o[:a] = 2

- assert\_equal(2, o[:a])

- assert\_raise(NameError) { o[:b] = 3 }

- end

- 
- def test\_struct\_new

- Struct.instance\_eval { const\_set(:Foo, nil) }

- assert\_raise(NameError) { Struct.new(“foo”) }

- assert\_nothing\_raised { Struct.new(“Foo”) }

- Struct.instance\_eval { remove\_const(:Foo) }

- assert\_nothing\_raised { Struct.new(:a) { } }

- assert\_raise(RuntimeError) { Struct.new(:a) { raise } }

- 
- assert\_equal([:utime, :stime, :cutime, :cstime],  
Process.times.members)

- end

- 
- def test\_initialize

- klass = Struct.new(:a)

- assert\_raise(ArgumentError) { klass.new(1, 2) }

- end

- 
- def test\_each

- klass = Struct.new(:a, :b)

- o = klass.new(1, 2)

- assert\_equal([1, 2], o.each.to\_a)

- end

- 
- def test\_each\_pair

- klass = Struct.new(:a, :b)

- o = klass.new(1, 2)

- assert\_equal([[:a, 1], [:b, 2]], o.each\_pair.to\_a)

- end

- 
- def test\_inspect

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_equal("#", o.inspect)

- o.a = o

- assert(o.inspect =~ /^#\<struct a=#\<struct #\<.\*?\>:…\>\>$/)

- 
- Struct.new(“Foo”, :a)

- o = Struct::Foo.new(1)

- assert\_equal("#", o.inspect)

- Struct.instance\_eval { remove\_const(:Foo) }

- 
- klass = Struct.new(:a, :b)

- o = klass.new(1, 2)

- assert\_equal("#", o.inspect)

- 
- klass = Struct.new(:@a)

- o = klass.new(1)

- assert\_equal("#\<struct :@a=1\>", o.inspect)

- end

- 
- def test\_init\_copy

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_equal(o, o.dup)

- end

- 
- def test\_aref

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_equal(1, o[0])

- assert\_raise(IndexError) { o[-2] }

- assert\_raise(IndexError) { o[1] }

- end

- 
- def test\_aset

- klass = Struct.new(:a)

- o = klass.new(1)

- o[0] = 2

- assert\_equal(2, o[:a])

- assert\_raise(IndexError) { o[-2] = 3 }

- assert\_raise(IndexError) { o[1] = 3 }

- end

- 
- def test\_values\_at

- klass = Struct.new(:a, :b, :c, :d, :e, :f)

- o = klass.new(1, 2, 3, 4, 5, 6)

- assert\_equal([2, 4, 6], o.values\_at(1, 3, 5))

- assert\_equal([2, 3, 4, 3, 4, 5], o.values\_at(1…3, 2…5))

- end

- 
- def test\_select

- klass = Struct.new(:a, :b, :c, :d, :e, :f)

- o = klass.new(1, 2, 3, 4, 5, 6)

- assert\_equal([1, 3, 5], o.select {|v| v % 2 != 0 })

- assert\_raise(ArgumentError) { o.select(1) }

- end

- 
- def test\_equal

- klass1 = Struct.new(:a)

- klass2 = Struct.new(:a, :b)

- o1 = klass1.new(1)

- o2 = klass1.new(1)

- o3 = klass2.new(1)

- assert(o1.==(o2))

- assert(o1 != o3)

- end

- 
- def test\_hash

- klass = Struct.new(:a)

- o = klass.new(1)

- assert(o.hash.is\_a?(Fixnum))

- end

- 
- def test\_eql

- klass1 = Struct.new(:a)

- klass2 = Struct.new(:a, :b)

- o1 = klass1.new(1)

- o2 = klass1.new(1)

- o3 = klass2.new(1)

- assert(o1.eql?(o2))

- assert(!(o1.eql?(o3)))

- end

- 
- def test\_size

- klass = Struct.new(:a)

- o = klass.new(1)

- assert\_equal(1, o.size)

- end  
end  
Index: test/ruby/test\_sprintf.rb  
===================================================================  
— test/ruby/test\_sprintf.rb (revision 15173)  
+++ test/ruby/test\_sprintf.rb (working copy)  
@@ -173,10 +173,88 @@  
assert\_equal("%\0x hello", sprintf("%\0x hello"),  
“[ruby-core:11571]”)

- 
- assert\_raise(ArgumentError) { sprintf("%\1", 1) }

- assert\_raise(ArgumentError) { sprintf("%!", 1) }

- assert\_raise(ArgumentError) { sprintf("%1$1$d", 1) }

- assert\_raise(ArgumentError) { sprintf("%0%") }

- assert\_nothing\_raised { sprintf("", 1) }  
end

def test\_float  
assert\_equal(“36893488147419111424”,  
sprintf("%20.0f", 36893488147419107329.0))  
end  
+

- BSIZ = 120
- 
- def test\_skip
- assert\_equal(" " \* BSIZ + “1”, sprintf(" " \* BSIZ + “%d”, 1))
- end
- 
- def test\_char
- assert\_equal(“a”, sprintf("%c", 97))
- assert\_equal(“a”, sprintf("%c", ?a))
- assert\_raise(ArgumentError) { sprintf("%c", sprintf("%c%c", ?a,  
?a)) }
- assert\_equal(" " \* (BSIZ - 1) + “a”, sprintf(" " \* (BSIZ - 1) +  
“%c”, ?a))
- assert\_equal(" " \* (BSIZ - 1) + “a”, sprintf(" " \* (BSIZ - 1) +  
“%-1c”, ?a))
- assert\_equal(" " \* BSIZ + “a”, sprintf("%#{ BSIZ + 1 }c", ?a))
- assert\_equal(“a” + " " \* BSIZ, sprintf("%-#{ BSIZ + 1 }c", ?a))
- end
- 
- def test\_string
- assert\_equal(“foo”, sprintf("%s", “foo”))
- assert\_equal(“fo”, sprintf("%.2s", “foo”))
- assert\_equal(" " \* BSIZ, sprintf("%s", " " \* BSIZ))
- assert\_equal(" " \* (BSIZ - 1) + “foo”, sprintf("%#{ BSIZ - 1 + 3  
}s", “foo”))
- assert\_equal(" " \* BSIZ + “foo”, sprintf("%#{ BSIZ + 3 }s", “foo”))
- assert\_equal(“foo” + " " \* BSIZ, sprintf("%-#{ BSIZ + 3 }s",  
“foo”))
- end
- 
- def test\_integer
- assert\_equal(“01”, sprintf("%#o", 1))
- assert\_equal(“0x1”, sprintf("%#x", 1))
- assert\_equal(“0X1”, sprintf("%#X", 1))
- assert\_equal(“0b1”, sprintf("%#b", 1))
- assert\_equal(“0B1”, sprintf("%#B", 1))
- assert\_equal(“1”, sprintf("%d", 1.0))
- assert\_equal(“4294967296”, sprintf("%d", (2\*\*32).to\_f))
- assert\_equal(“1”, sprintf("%d", “1”))
- o = Object.new; def o.to\_int; 1; end
- assert\_equal(“1”, sprintf("%d", o))
- assert\_equal("+1", sprintf("%+d", 1))
- assert\_equal(" 1", sprintf("% d", 1))
- assert\_equal("…f", sprintf("%x", -1))
- assert\_equal("…7", sprintf("%o", -1))
- one = (2\*\*32).coerce(1).first
- mone = (2\*\*32).coerce(-1).first
- assert\_equal("+1", sprintf("%+d", one))
- assert\_equal(" 1", sprintf("% d", one))
- assert\_equal("…f", sprintf("%x", mone))
- assert\_equal("…7", sprintf("%o", mone))
- assert\_equal(" " \* BSIZ + “1”, sprintf("%#{ BSIZ + 1 }d", one))
- assert\_equal(" " \* (BSIZ - 1) + “1”, sprintf(" " \* (BSIZ - 1) +  
“%d”, 1))
- end
- 
- def test\_float2
- inf = 1.0 / 0.0
- assert\_equal(" " \* BSIZ + “Inf”, sprintf("%#{ BSIZ + 3 }.1f", inf))
- assert\_equal("+Inf", sprintf("%±f", inf))
- assert\_equal(" " \* BSIZ + “1.0”, sprintf("%#{ BSIZ + 3 }.1f", 1.0))
- end
- 
- class  
T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
- end
- 
- def test\_star
- assert\_equal("-1 “, sprintf(”%\*d", -3, -1))
- end
- 
- def test\_escape
- assert\_equal("%" \* BSIZ, sprintf("%%" \* BSIZ))
- end
- 
- def test\_rb\_sprintf
- 

assert(T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.new.inspect  
=~  
/^#TestSprintf::T012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:0x[0-9a-f]+$/)

- end  
end  
Index: test/ruby/test\_math.rb  
===================================================================  
— test/ruby/test\_math.rb (revision 15173)  
+++ test/ruby/test\_math.rb (working copy)  
@@ -1,12 +1,173 @@  
require ‘test/unit’

class TestMath \< Test::Unit::TestCase

- def test\_math
- assert\_equal(2, Math.sqrt(4))

- def check(a, b)
- assert(a - b \< Float::EPSILON \* 3)
- end

- self.class.class\_eval {
- 

```
 include Math

```

- }
- assert\_equal(2, sqrt(4))

- def test\_atan2
- check(Math.atan2(0, 1), 0)
- check(Math.atan2(1, 1), Math::PI / 4)
- check(Math.atan2(1, 0), Math::PI / 2)  
end
- 
- def test\_cos
- check(Math.cos(0 \* Math::PI / 4), 1.0)
- check(Math.cos(1 \* Math::PI / 4), 1.0 / Math.sqrt(2))
- check(Math.cos(2 \* Math::PI / 4), 0.0)
- check(Math.cos(4 \* Math::PI / 4), -1.0)
- check(Math.cos(6 \* Math::PI / 4), 0.0)
- end
- 
- def test\_sin
- check(Math.sin(0 \* Math::PI / 4), 0.0)
- check(Math.sin(1 \* Math::PI / 4), 1.0 / Math.sqrt(2))
- check(Math.sin(2 \* Math::PI / 4), 1.0)
- check(Math.sin(4 \* Math::PI / 4), 0.0)
- check(Math.sin(6 \* Math::PI / 4), -1.0)
- end
- 
- def test\_tan
- check(Math.tan(0 \* Math::PI / 4), 0.0)
- check(Math.tan(1 \* Math::PI / 4), 1.0)
- assert(Math.tan(2 \* Math::PI / 4).abs \> 1024)
- check(Math.tan(4 \* Math::PI / 4), 0.0)
- assert(Math.tan(6 \* Math::PI / 4).abs \> 1024)
- end
- 
- def test\_acos
- check(0 \* Math::PI / 4, Math.acos( 1.0))
- check(1 \* Math::PI / 4, Math.acos( 1.0 / Math.sqrt(2)))
- check(2 \* Math::PI / 4, Math.acos( 0.0))
- check(4 \* Math::PI / 4, Math.acos(-1.0))
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.acos(2.0) }
- end
- 
- def test\_asin
- check( 0 \* Math::PI / 4, Math.asin( 0.0))
- check( 1 \* Math::PI / 4, Math.asin( 1.0 / Math.sqrt(2)))
- check( 2 \* Math::PI / 4, Math.asin( 1.0))
- check(-2 \* Math::PI / 4, Math.asin(-1.0))
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.asin(2.0) }
- end
- 
- def test\_atan
- check( 0 \* Math::PI / 4, Math.atan( 0.0))
- check( 1 \* Math::PI / 4, Math.atan( 1.0))
- check( 2 \* Math::PI / 4, Math.atan(1.0 / 0.0))
- check(-1 \* Math::PI / 4, Math.atan(-1.0))
- end
- 
- def test\_cosh
- check(1, Math.cosh(0))
- check((Math::E \*\* 1 + Math::E \*\* -1) / 2, Math.cosh(1))
- check((Math::E \*\* 2 + Math::E \*\* -2) / 2, Math.cosh(2))
- end
- 
- def test\_sinh
- check(0, Math.sinh(0))
- check((Math::E \*\* 1 - Math::E \*\* -1) / 2, Math.sinh(1))
- check((Math::E \*\* 2 - Math::E \*\* -2) / 2, Math.sinh(2))
- end
- 
- def test\_tanh
- check(Math.sinh(0) / Math.cosh(0), Math.tanh(0))
- check(Math.sinh(1) / Math.cosh(1), Math.tanh(1))
- check(Math.sinh(2) / Math.cosh(2), Math.tanh(2))
- end
- 
- def test\_acosh
- check(Math.acosh(1), 0)
- check(Math.acosh((Math::E \*\* 1 + Math::E \*\* -1) / 2), 1)
- check(Math.acosh((Math::E \*\* 2 + Math::E \*\* -2) / 2), 2)
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.acosh(0) }
- end
- 
- def test\_asinh
- check(Math.asinh(0), 0)
- check(Math.asinh((Math::E \*\* 1 - Math::E \*\* -1) / 2), 1)
- check(Math.asinh((Math::E \*\* 2 - Math::E \*\* -2) / 2), 2)
- end
- 
- def test\_atanh
- check(Math.atanh(Math.sinh(0) / Math.cosh(0)), 0)
- check(Math.atanh(Math.sinh(1) / Math.cosh(1)), 1)
- check(Math.atanh(Math.sinh(2) / Math.cosh(2)), 2)
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.atanh(-1) }
- end
- 
- def test\_exp
- check(1, Math.exp(0))
- check(Math.sqrt(Math::E), Math.exp(0.5))
- check(Math::E, Math.exp(1))
- check(Math::E \*\* 2, Math.exp(2))
- end
- 
- def test\_log
- check(0, Math.log(1))
- check(1, Math.log(Math::E))
- check(0, Math.log(1, 10))
- check(1, Math.log(10, 10))
- check(2, Math.log(100, 10))
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.log(0) }
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.log(-1) }
- end
- 
- def test\_log2
- check(0, Math.log2(1))
- check(1, Math.log2(2))
- check(2, Math.log2(4))
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(0) }
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.log2(-1) }
- end
- 
- def test\_log10
- check(0, Math.log10(1))
- check(1, Math.log10(10))
- check(2, Math.log10(100))
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(0) }
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.log10(-1) }
- end
- 
- def test\_sqrt
- check(0, Math.sqrt(0))
- check(1, Math.sqrt(1))
- check(2, Math.sqrt(4))
- assert\_raise(Errno::EDOM, Errno::ERANGE) { Math.sqrt(-1) }
- end
- 
- def test\_frexp
- check(0.0, Math.frexp(0.0).first)
- assert\_equal(0, Math.frexp(0).last)
- check(0.5, Math.frexp(0.5).first)
- assert\_equal(0, Math.frexp(0.5).last)
- check(0.5, Math.frexp(1.0).first)
- assert\_equal(1, Math.frexp(1.0).last)
- check(0.5, Math.frexp(2.0).first)
- assert\_equal(2, Math.frexp(2.0).last)
- check(0.75, Math.frexp(3.0).first)
- assert\_equal(2, Math.frexp(3.0).last)
- end
- 
- def test\_ldexp
- check(0.0, Math.ldexp(0.0, 0.0))
- check(0.5, Math.ldexp(0.5, 0.0))
- check(1.0, Math.ldexp(0.5, 1.0))
- check(2.0, Math.ldexp(0.5, 2.0))
- check(3.0, Math.ldexp(0.75, 2.0))
- end
- 
- def test\_hypot
- check(5, Math.hypot(3, 4))
- end
- 
- def test\_erf
- check(0, Math.erf(0))
- check(1, Math.erf(1.0 / 0.0))
- end
- 
- def test\_erfc
- check(1, Math.erfc(0))
- check(0, Math.erfc(1.0 / 0.0))
- end  
end

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [January 23, 2008, 5:01am UTC](https://www.ruby-forum.com/t/exhaustive-tests-for-struct-c-sprintf-c-and-math-c/126826/2 "2008-01-23T05:01:39Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:33280] exhaustive tests for struct.c,  
sprintf.c and math.c”  
on Wed, 23 Jan 2008 00:03:52 +0900, “Yusuke ENDOH” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|test\_struct.rb e$B$He(B test\_sprintf.rb e$B$He(B test\_math.rb e$B$K$b%F%9%H$rDI2Ce(B  
|e$B$7$^$7$?!#e(Bgcov e$B$G$N%+%P%l%C%8$,0J2\<$N$h$&$K\>e$,$j$^$9!#e(B  
|  
|struct.c : 55.87% → 94.60%  
|sprintf.c : 70.45% → 97.50%  
|math.c : 28.36% → 98.51%  
|  
|e$B$3$l$b%3%\_%C%H$7$F$h$m$7$$$G$7$g$&$+!#$h$1$l$P$=$N$&$AB\>$G$b$d$m$&$He(B  
|e$B;W$$$^$9!#e(B

e$B$=$&$G$9$M!#$=$NB\>$N%F%9%H$b1sN8$J$/DI2C$7$F$/$@$5$$!#$?$@!"e(B  
e$B%F%9%H;~4V$,0[MM$KD9$/$J$i$J$$DxEY$G:Q$`$H=u$+$j$^$9!#e(B
