File: crc32_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (74 lines) | stat: -rw-r--r-- 1,924 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require "../spec_helper"
require "./spec_helper"
require "digest/crc32"

describe Digest::CRC32 do
  it_acts_as_digest_algorithm Digest::CRC32

  it "calculates digest from string" do
    Digest::CRC32.digest("foo").to_slice.should eq Bytes[0x8c, 0x73, 0x65, 0x21]
  end

  it "calculates hash from string" do
    Digest::CRC32.hexdigest("foo").should eq("8c736521")
  end

  it "calculates hash from unicode string" do
    Digest::CRC32.hexdigest("fooø").should eq("d5f3a18b")
  end

  it "calculates hash from UInt8 slices" do
    s = Bytes[0x66, 0x6f, 0x6f] # f,o,o
    Digest::CRC32.hexdigest(s).should eq("8c736521")
  end

  it "calculates hash of #to_slice" do
    buffer = StaticArray(UInt8, 1).new(1_u8)
    Digest::CRC32.hexdigest(buffer).should eq("a505df1b")
  end

  it "can take a block" do
    Digest::CRC32.hexdigest do |ctx|
      ctx.update "f"
      ctx.update Bytes[0x6f, 0x6f]
    end.should eq("8c736521")
  end

  it "calculates base64'd hash from string" do
    Digest::CRC32.base64digest("foo").should eq("jHNlIQ==")
  end

  it "resets" do
    digest = Digest::CRC32.new
    digest.update "foo"
    digest.final.hexstring.should eq("8c736521")

    digest.reset
    digest.update "foo"
    digest.final.hexstring.should eq("8c736521")
  end

  it "can't call final twice" do
    digest = Digest::CRC32.new
    digest.final
    expect_raises(Digest::FinalizedError) do
      digest.final
    end
  end

  it "return the digest size" do
    Digest::CRC32.new.digest_size.should eq 4
  end

  it "should be able to calculate crc32" do
    crc = Digest::CRC32.checksum("foo").to_s(16)
    crc.should eq("8c736521")
  end

  it "should be able to calculate crc32 combined" do
    crc1 = Digest::CRC32.checksum("hello")
    crc2 = Digest::CRC32.checksum(" world!")
    combined = Digest::CRC32.combine(crc1, crc2, " world!".size)
    Digest::CRC32.checksum("hello world!").should eq(combined)
  end
end