Skip to content

Commit

Permalink
define equals and hash on bitfield
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrberry committed Oct 20, 2020
1 parent 2cac0ae commit 993eb7f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: crystal

# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
script:
- crystal spec
- crystal tool format --check
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# BitField - Fixed Length Bitfields in Crystal Lang

[![Build Status](https://travis-ci.org/mattrberry/bitfield.svg?branch=master)](https://travis-ci.org/mattrberry/bitfield)


The goal that BitField strives to accomplish is to allow for the creation of minimal-effort bitfields in Crystal. The intention is not intended to interop with C bitfields.

## Installation
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: bitfield
version: 0.1.1
version: 0.1.2

authors:
- Matthew Berry <[email protected]>
Expand Down
16 changes: 16 additions & 0 deletions spec/bitfield_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,20 @@ describe BitField do
bf.double_bits
bf.bits.should eq 0b00000010
end

it "defines equals" do
bf = Test8.new(0xAF)
bf.should eq Test8.new(0xAF)
Test8.new(0xAF).should eq Test8.new(0xAF)
bf.should_not eq Test8.new(0xFA)
Test8.new(0xAF).should_not eq Test8.new(0xFA)
end

it "defines hash" do
bf = Test8.new(0xAF)
bf.hash.should eq Test8.new(0xAF).hash
Test8.new(0xAF).hash.should eq Test8.new(0xAF).hash
bf.hash.should_not eq Test8.new(0xFA).hash
Test8.new(0xAF).hash.should_not eq Test8.new(0xFA).hash
end
end
2 changes: 2 additions & 0 deletions src/bitfield.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ abstract class BitField(T)
bits = sizeof(T) * 8
raise "You must describe exactly #{bits} bits (#{SIZE} bits have been described)" unless SIZE == bits
end

def_equals_and_hash @value
end
end

Expand Down

0 comments on commit 993eb7f

Please sign in to comment.