|
| 1 | +package simple_cipher |
| 2 | + |
| 3 | +import "core:strings" |
| 4 | +import "core:testing" |
| 5 | + |
| 6 | +@(test) |
| 7 | +/// description = Random key cipher -> Can encode |
| 8 | +test_random_key_cipher___can_encode :: proc(t: ^testing.T) { |
| 9 | + |
| 10 | + plaintext := "aaaaaaaaaa" |
| 11 | + random_key := key() |
| 12 | + result := encode(plaintext, random_key) |
| 13 | + expected, _ := strings.substring(random_key, 0, len(plaintext)) |
| 14 | + defer { |
| 15 | + delete(random_key) |
| 16 | + delete(result) |
| 17 | + } |
| 18 | + |
| 19 | + testing.expectf( |
| 20 | + t, |
| 21 | + len(random_key) >= 100, |
| 22 | + "key() returns key of less than 100 characters (%d)", |
| 23 | + len(random_key), |
| 24 | + ) |
| 25 | + testing.expect_value(t, result, expected) |
| 26 | +} |
| 27 | + |
| 28 | +@(test) |
| 29 | +/// description = Random key cipher -> Can decode |
| 30 | +test_random_key_cipher___can_decode :: proc(t: ^testing.T) { |
| 31 | + |
| 32 | + random_key := key() |
| 33 | + expected := "aaaaaaaaaa" |
| 34 | + ciphertext, _ := strings.substring(random_key, 0, len(expected)) |
| 35 | + result := decode(ciphertext, random_key) |
| 36 | + defer { |
| 37 | + delete(random_key) |
| 38 | + delete(result) |
| 39 | + } |
| 40 | + |
| 41 | + testing.expectf( |
| 42 | + t, |
| 43 | + len(random_key) >= 100, |
| 44 | + "key() returns key of less than 100 characters (%d)", |
| 45 | + len(random_key), |
| 46 | + ) |
| 47 | + testing.expect_value(t, result, expected) |
| 48 | +} |
| 49 | + |
| 50 | +@(test) |
| 51 | +/// description = Random key cipher -> Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method |
| 52 | +test_random_key_cipher___is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method :: proc( |
| 53 | + t: ^testing.T, |
| 54 | +) { |
| 55 | + |
| 56 | + random_key := key() |
| 57 | + plaintext := "abcdefghij" |
| 58 | + ciphertext := encode(plaintext, random_key) |
| 59 | + result := decode(ciphertext, random_key) |
| 60 | + defer { |
| 61 | + delete(random_key) |
| 62 | + delete(ciphertext) |
| 63 | + delete(result) |
| 64 | + } |
| 65 | + |
| 66 | + testing.expect_value(t, result, plaintext) |
| 67 | +} |
| 68 | + |
| 69 | +@(test) |
| 70 | +/// description = Random key cipher -> Key is made only of lowercase letters |
| 71 | +test_random_key_cipher___key_is_made_only_of_lowercase_letters :: proc(t: ^testing.T) { |
| 72 | + |
| 73 | + random_key := key() |
| 74 | + expected := strings.to_lower(random_key) |
| 75 | + defer { |
| 76 | + delete(random_key) |
| 77 | + delete(expected) |
| 78 | + } |
| 79 | + |
| 80 | + testing.expect_value(t, random_key, expected) |
| 81 | +} |
| 82 | + |
| 83 | +@(test) |
| 84 | +/// description = Substitution cipher -> Can encode |
| 85 | +test_substitution_cipher___can_encode :: proc(t: ^testing.T) { |
| 86 | + |
| 87 | + key := "abcdefghij" |
| 88 | + plaintext := "aaaaaaaaaa" |
| 89 | + result := encode(plaintext, key) |
| 90 | + expected := "abcdefghij" |
| 91 | + defer delete(result) |
| 92 | + |
| 93 | + testing.expect_value(t, result, expected) |
| 94 | +} |
| 95 | + |
| 96 | +@(test) |
| 97 | +/// description = Substitution cipher -> Can decode |
| 98 | +test_substitution_cipher___can_decode :: proc(t: ^testing.T) { |
| 99 | + |
| 100 | + key := "abcdefghij" |
| 101 | + ciphertext := "abcdefghij" |
| 102 | + result := decode(ciphertext, key) |
| 103 | + expected := "aaaaaaaaaa" |
| 104 | + defer delete(result) |
| 105 | + |
| 106 | + testing.expect_value(t, result, expected) |
| 107 | +} |
| 108 | + |
| 109 | +@(test) |
| 110 | +/// description = Substitution cipher -> Is reversible. I.e., if you apply decode in a encoded result, you must see the same plaintext encode parameter as a result of the decode method |
| 111 | +test_substitution_cipher___is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method :: proc( |
| 112 | + t: ^testing.T, |
| 113 | +) { |
| 114 | + |
| 115 | + key := "abcdefghij" |
| 116 | + plaintext := "abcdefghij" |
| 117 | + ciphertext := encode(plaintext, key) |
| 118 | + result := decode(ciphertext, key) |
| 119 | + defer { |
| 120 | + delete(ciphertext) |
| 121 | + delete(result) |
| 122 | + } |
| 123 | + |
| 124 | + testing.expect_value(t, result, plaintext) |
| 125 | +} |
| 126 | + |
| 127 | +@(test) |
| 128 | +/// description = Substitution cipher -> Can double shift encode |
| 129 | +test_substitution_cipher___can_double_shift_encode :: proc(t: ^testing.T) { |
| 130 | + |
| 131 | + key := "iamapandabear" |
| 132 | + plaintext := "iamapandabear" |
| 133 | + result := encode(plaintext, key) |
| 134 | + expected := "qayaeaagaciai" |
| 135 | + defer delete(result) |
| 136 | + |
| 137 | + testing.expect_value(t, result, expected) |
| 138 | +} |
| 139 | + |
| 140 | +@(test) |
| 141 | +/// description = Substitution cipher -> Can wrap on encode |
| 142 | +test_substitution_cipher___can_wrap_on_encode :: proc(t: ^testing.T) { |
| 143 | + |
| 144 | + key := "abcdefghij" |
| 145 | + plaintext := "zzzzzzzzzz" |
| 146 | + result := encode(plaintext, key) |
| 147 | + expected := "zabcdefghi" |
| 148 | + defer delete(result) |
| 149 | + |
| 150 | + testing.expect_value(t, result, expected) |
| 151 | +} |
| 152 | + |
| 153 | +@(test) |
| 154 | +/// description = Substitution cipher -> Can wrap on decode |
| 155 | +test_substitution_cipher___can_wrap_on_decode :: proc(t: ^testing.T) { |
| 156 | + |
| 157 | + key := "abcdefghij" |
| 158 | + ciphertext := "zabcdefghi" |
| 159 | + result := decode(ciphertext, key) |
| 160 | + expected := "zzzzzzzzzz" |
| 161 | + defer delete(result) |
| 162 | + |
| 163 | + testing.expect_value(t, result, expected) |
| 164 | +} |
| 165 | + |
| 166 | +@(test) |
| 167 | +/// description = Substitution cipher -> Can encode messages longer than the key |
| 168 | +test_substitution_cipher___can_encode_messages_longer_than_the_key :: proc(t: ^testing.T) { |
| 169 | + |
| 170 | + key := "abc" |
| 171 | + plaintext := "iamapandabear" |
| 172 | + result := encode(plaintext, key) |
| 173 | + expected := "iboaqcnecbfcr" |
| 174 | + defer delete(result) |
| 175 | + |
| 176 | + testing.expect_value(t, result, expected) |
| 177 | +} |
| 178 | + |
| 179 | +@(test) |
| 180 | +/// description = Substitution cipher -> Can decode messages longer than the key |
| 181 | +test_substitution_cipher___can_decode_messages_longer_than_the_key :: proc(t: ^testing.T) { |
| 182 | + |
| 183 | + key := "abc" |
| 184 | + ciphertext := "iboaqcnecbfcr" |
| 185 | + result := decode(ciphertext, key) |
| 186 | + expected := "iamapandabear" |
| 187 | + defer delete(result) |
| 188 | + |
| 189 | + testing.expect_value(t, result, expected) |
| 190 | +} |
0 commit comments