Skip to content

Fix: Implement = and hash#17

Open
HossamSaberr wants to merge 2 commits intopharo-containers:masterfrom
HossamSaberr:master
Open

Fix: Implement = and hash#17
HossamSaberr wants to merge 2 commits intopharo-containers:masterfrom
HossamSaberr:master

Conversation

@HossamSaberr
Copy link

Fix #16.

Previously, CTQueue inherited Object's default identity comparison, meaning two queues with the exact same elements were not considered equal.

Changes made:

  • Implemented #= in CTQueue to compare species, size, and internal elements.
  • Implemented #hash in CTQueue using the standard collection hash multiplier.
  • Added testEquality to CTQueueTest to verify that identical queues evaluate to true and their hashes match.

All tests are Green now

image

]

{ #category : 'tests' }
CTQueueTest >> testEnqueueUnless [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split this test? Normally tests should be descriptive

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

CTQueue >> = anObject [

self == anObject ifTrue: [ ^ true ].
self species == anObject species ifFalse: [ ^ false ].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the species comparison is necessary here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially mimicking how standard Pharo collections use species for their equality checks. But I realize now that since CTQueue inherits directly from Object, using species is too loose and could cause unexpected behavior if the queue is subclassed. i pushed an updated for that

{ #category : 'comparing' }
CTQueue >> = anObject [

self == anObject ifTrue: [ ^ true ].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. First you should compare if they have the same class

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i pushed an updated for that

CTQueue >> hash [

| hashValue |
hashValue := self species hash.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why self species hash?


| hashValue |
hashValue := self species hash.
self do: [ :each | hashValue := (hashValue + each hash) hashMultiply ].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the hash function looks weird. We need to test it to see the collisions, etc. We need some benchmarks for that. They do not need to be in the tests but we should make sure that is a good hash function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing equals and hash implementation

2 participants