File: crc32_spec.rb

package info (click to toggle)
ruby3.1 3.1.2-7%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 132,892 kB
  • sloc: ruby: 1,154,753; ansic: 736,782; yacc: 46,445; pascal: 10,401; sh: 3,931; cpp: 1,158; python: 838; makefile: 787; asm: 462; javascript: 382; lisp: 97; sed: 94; perl: 62; awk: 36; xml: 4
file content (54 lines) | stat: -rw-r--r-- 2,370 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
54
require_relative '../../spec_helper'
require 'zlib'

describe "Zlib.crc32" do
  it "calculates CRC checksum for string" do
    Zlib.crc32("").should == 0
    Zlib.crc32(" ").should == 3916222277
    Zlib.crc32("123456789").should == 3421780262
    Zlib.crc32("!@#\{$\}%^&**()").should == 2824518887
    Zlib.crc32("to be or not to be" * 22).should == 1832379978
    Zlib.crc32("0").should == 4108050209
    Zlib.crc32((2**32).to_s).should == 3267533297
    Zlib.crc32((2**64).to_s).should == 653721760
  end

  it "calculates CRC checksum for string and initial CRC value" do
    test_string = "This is a test string! How exciting!%?"
    # Zlib.crc32(test_string, -2**28).should == 3230195786
    # Zlib.crc32(test_string, -2**20).should == 2770207303
    # Zlib.crc32(test_string, -2**16).should == 2299432960
    # Zlib.crc32(test_string, -2**8).should == 861809849
    # Zlib.crc32(test_string, -1).should == 2170124077
    Zlib.crc32(test_string, 0).should == 3864990561
    Zlib.crc32(test_string, 1).should == 1809313411
    Zlib.crc32(test_string, 2**8).should == 1722745982
    Zlib.crc32(test_string, 2**16).should == 1932511220
    Zlib.crc32("p", ~305419896).should == 4046865307
    Zlib.crc32("p", -305419897).should == 4046865307
    -> { Zlib.crc32(test_string, 2**128) }.should raise_error(RangeError)
  end

  it "calculates the CRC checksum for string and initial CRC value for Integers" do
    test_string = "This is a test string! How exciting!%?"
    # Zlib.crc32(test_string, -2**30).should == 277228695
    Zlib.crc32(test_string, 2**30).should == 46597132
  end

  it "assumes that the initial value is given to crc, if crc is omitted" do
    orig_crc = Zlib.crc32
    Zlib.crc32("").should == Zlib.crc32("", orig_crc)
    Zlib.crc32(" ").should == Zlib.crc32(" ", orig_crc)
    Zlib.crc32("123456789").should == Zlib.crc32("123456789", orig_crc)
    Zlib.crc32("!@#\{$\}%^&**()").should == Zlib.crc32("!@#\{$\}%^&**()", orig_crc)
    Zlib.crc32("to be or not to be" * 22).should == Zlib.crc32("to be or not to be" * 22, orig_crc)
    Zlib.crc32("0").should == Zlib.crc32("0", orig_crc)
    Zlib.crc32((2**32).to_s).should == Zlib.crc32((2**32).to_s, orig_crc)
    Zlib.crc32((2**64).to_s).should == Zlib.crc32((2**64).to_s, orig_crc)
  end

  it "it returns the CRC initial value, if string is omitted" do
    Zlib.crc32.should == 0
  end

end