File: gcdlcm_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (53 lines) | stat: -rw-r--r-- 1,587 bytes parent folder | download | duplicates (6)
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
require_relative '../../spec_helper'

describe "Integer#gcdlcm" do
  it "returns [self, self] if self is equal to the argument" do
    1.gcdlcm(1).should == [1, 1]
    398.gcdlcm(398).should == [398, 398]
  end

  it "returns an Array" do
    36.gcdlcm(6).should be_kind_of(Array)
    4.gcdlcm(20981).should be_kind_of(Array)
  end

  it "returns a two-element Array" do
    36.gcdlcm(876).size.should == 2
    29.gcdlcm(17).size.should == 2
  end

  it "returns the greatest common divisor of self and argument as the first element" do
    10.gcdlcm(5)[0].should == 10.gcd(5)
    200.gcdlcm(20)[0].should == 200.gcd(20)
  end

  it "returns the least common multiple of self and argument as the last element" do
    10.gcdlcm(5)[1].should == 10.lcm(5)
    200.gcdlcm(20)[1].should == 200.lcm(20)
  end

  it "accepts a Bignum argument" do
    bignum = 91999**99
    bignum.should be_kind_of(Integer)
    99.gcdlcm(bignum).should == [99.gcd(bignum), 99.lcm(bignum)]
  end

  it "works if self is a Bignum" do
    bignum = 9999**89
    bignum.should be_kind_of(Integer)
    bignum.gcdlcm(99).should == [bignum.gcd(99), bignum.lcm(99)]
  end

  it "raises an ArgumentError if not given an argument" do
    -> { 12.gcdlcm }.should raise_error(ArgumentError)
  end

  it "raises an ArgumentError if given more than one argument" do
    -> { 12.gcdlcm(30, 20) }.should raise_error(ArgumentError)
  end

  it "raises a TypeError unless the argument is an Integer" do
    -> { 39.gcdlcm(3.8)   }.should raise_error(TypeError)
    -> { 45872.gcdlcm([]) }.should raise_error(TypeError)
  end
end