diff --git a/src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js b/src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js index 90521cedf8..86f8bbe768 100644 --- a/src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js +++ b/src/algorithms/cryptography/caesar-cipher/__test__/caesarCipher.test.js @@ -1,40 +1,101 @@ +(Sem assunto) +teo machado +VocĂȘ import { caesarCipherEncrypt, caesarCipherDecrypt } from '../caesarCipher'; describe('caesarCipher', () => { - it('should not change a string with zero shift', () => { - expect(caesarCipherEncrypt('abcd', 0)).toBe('abcd'); - expect(caesarCipherDecrypt('abcd', 0)).toBe('abcd'); - }); +describe('caesarCipherEncrypt', () => { +it.each([ +{ input: 'abcd', shift: 0, expected: 'abcd' }, +{ input: 'abcde', shift: 1, expected: 'bcdef' }, +{ input: 'abcde', shift: 3, expected: 'defgh' }, +{ input: 'xyz', shift: 1, expected: 'yza' }, +{ input: 'z', shift: 1, expected: 'a' }, +{ input: 'ABCDE', shift: 3, expected: 'defgh' }, +{ input: '', shift: 3, expected: '' }, +{ input: 'ab2cde', shift: 3, expected: 'de2fgh' }, +{ input: 'Hello, World!', shift: 3, expected: 'khoor, zruog!' }, +])( +'encrypts " - it('should cipher a string with different shifts', () => { - expect(caesarCipherEncrypt('abcde', 3)).toBe('defgh'); - expect(caesarCipherDecrypt('defgh', 3)).toBe('abcde'); +shift', +({ input, shift, expected }) => { +expect(caesarCipherEncrypt(input, shift)).toBe(expected); +}, +); - expect(caesarCipherEncrypt('abcde', 1)).toBe('bcdef'); - expect(caesarCipherDecrypt('bcdef', 1)).toBe('abcde'); +it('should preserve non-alphabetic characters', () => { +expect(caesarCipherEncrypt('123 !@#', 5)).toBe('123 !@#'); +}); - expect(caesarCipherEncrypt('xyz', 1)).toBe('yza'); - expect(caesarCipherDecrypt('yza', 1)).toBe('xyz'); - }); +it('should handle shifts greater than the alphabet length', () => { +expect(caesarCipherEncrypt('abc', 27)).toBe('bcd'); +}); - it('should be case insensitive', () => { - expect(caesarCipherEncrypt('ABCDE', 3)).toBe('defgh'); - }); +it('should handle a negative shift', () => { +expect(caesarCipherEncrypt('abc', -1)).toBe('zab'); +}); +}); - it('should correctly handle an empty strings', () => { - expect(caesarCipherEncrypt('', 3)).toBe(''); - }); +describe('caesarCipherDecrypt', () => { +it.each([ +{ input: 'abcd', shift: 0, expected: 'abcd' }, +{ input: 'bcdef', shift: 1, expected: 'abcde' }, +{ input: 'defgh', shift: 3, expected: 'abcde' }, +{ input: 'yza', shift: 1, expected: 'xyz' }, +{ input: 'a', shift: 1, expected: 'z' }, +{ input: '', shift: 3, expected: '' }, +{ input: 'de2fgh', shift: 3, expected: 'ab2cde' }, +{ +input: 'khoor, zruog!', +shift: 3, +expected: 'hello, world!', +}, +])( +'decrypts " - it('should not cipher unknown chars', () => { - expect(caesarCipherEncrypt('ab2cde', 3)).toBe('de2fgh'); - expect(caesarCipherDecrypt('de2fgh', 3)).toBe('ab2cde'); - }); +shift', +({ input, shift, expected }) => { +expect(caesarCipherDecrypt(input, shift)).toBe(expected); +}, +); - it('should encrypt and decrypt full phrases', () => { - expect(caesarCipherEncrypt('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)) - .toBe('qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald'); +it('should handle shifts greater than the alphabet length', () => { +expect(caesarCipherDecrypt('bcd', 27)).toBe('abc'); +}); - expect(caesarCipherDecrypt('qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald', 23)) - .toBe('the quick brown fox jumps over the lazy dog'); - }); +it('should handle a negative shift', () => { +expect(caesarCipherDecrypt('zab', -1)).toBe('abc'); +}); +}); + +describe('encryption and decryption round trip', () => { +it.each([ +{ input: 'hello world', shift: 3 }, +{ input: 'the quick brown fox', shift: 23 }, +{ input: 'abc xyz', shift: 26 }, +{ input: 'Text with 123 symbols!', shift: 7 }, +{ input: '', shift: 10 }, +])( +'should recover the original text for shift $shift', +({ input, shift }) => { +const encrypted = caesarCipherEncrypt(input, shift); +const decrypted = caesarCipherDecrypt(encrypted, shift); + +expect(decrypted).toBe(input.toLowerCase()); +}, +); +}); + +describe('full phrase', () => { +it('should encrypt and decrypt the pangram correctly', () => { +const original = 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'; +const encrypted = 'qeb nrfzh yoltk clu grjmp lsbo qeb ixwv ald'; + +expect(caesarCipherEncrypt(original, 23)).toBe(encrypted); +expect(caesarCipherDecrypt(encrypted, 23)).toBe( +'the quick brown fox jumps over the lazy dog', +); +}); }); +});