-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 30 additions & 9 deletions
39
src/algorithms/cryptography/hill-cipher/_test_/hillCipher.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,46 @@ | ||
import hillCipherEncrypt from '../hillCipher'; | ||
import { hillCipherEncrypt, hillCipherDecrypt } from '../hillCipher'; | ||
|
||
describe('hillCipher', () => { | ||
it('should throw an error when the length of the keyString does not equal to the power of length of the message ', () => { | ||
const invalidLenghOfkeyString = () => { | ||
hillCipherEncrypt('hello', 'helloworld'); | ||
}; | ||
|
||
expect(invalidLenghOfkeyString).toThrowError(); | ||
it('should throw an exception when trying to decipher', () => { | ||
expect(hillCipherDecrypt).toThrowError('This method is not implemented yet'); | ||
}); | ||
|
||
it('should throw an error when message or keyString contains none letter character', () => { | ||
const invalidCharacterInMessage = () => { | ||
hillCipherEncrypt('hell3', 'helloworld'); | ||
}; | ||
const invalidCharacterInKeyString = () => { | ||
hillCipherEncrypt('hello', 'hel12world'); | ||
}; | ||
expect(invalidCharacterInMessage).toThrowError(); | ||
expect(invalidCharacterInKeyString).toThrowError(); | ||
expect(invalidCharacterInMessage).toThrowError( | ||
'The message and key string can only contain letters', | ||
); | ||
expect(invalidCharacterInKeyString).toThrowError( | ||
'The message and key string can only contain letters', | ||
); | ||
}); | ||
|
||
it('should throw an error when the length of the keyString has a square root which is not integer', () => { | ||
const invalidLengthOfKeyString = () => { | ||
hillCipherEncrypt('ab', 'ab'); | ||
}; | ||
expect(invalidLengthOfKeyString).toThrowError( | ||
'Invalid key string length. The square root of the key string must be an integer', | ||
); | ||
}); | ||
|
||
it('should throw an error when the length of the keyString does not equal to the power of length of the message', () => { | ||
const invalidLengthOfKeyString = () => { | ||
hillCipherEncrypt('ab', 'aaabbbccc'); | ||
}; | ||
expect(invalidLengthOfKeyString).toThrowError( | ||
'Invalid key string length. The key length must be a square of message length', | ||
); | ||
}); | ||
|
||
it('should encrypt passed message using Hill Cipher', () => { | ||
expect(hillCipherEncrypt('ACT', 'GYBNQKURP')).toBe('POH'); | ||
expect(hillCipherEncrypt('CAT', 'GYBNQKURP')).toBe('FIN'); | ||
expect(hillCipherEncrypt('GFG', 'HILLMAGIC')).toBe('SWK'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters