|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2026 PCSX-Redux authors |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include "psyqo/adler32.hh" |
| 28 | + |
| 29 | +#include "snitch_all.hpp" |
| 30 | + |
| 31 | +using namespace psyqo; |
| 32 | + |
| 33 | +// --- Basic checksums --- |
| 34 | + |
| 35 | +TEST_CASE("Adler32 empty buffer") { |
| 36 | + uint32_t sum = adler32(nullptr, 0); |
| 37 | + REQUIRE(sum == 1); |
| 38 | +} |
| 39 | + |
| 40 | +TEST_CASE("Adler32 single byte") { |
| 41 | + uint8_t data[] = {0x01}; |
| 42 | + uint32_t sum = adler32(data, 1); |
| 43 | + // Initial: a=1, b=0. After byte 1: a = 1+1 = 2, b = 0+2 = 2. |
| 44 | + REQUIRE(sum == ((2 << 16) | 2)); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_CASE("Adler32 known vector: 123456789") { |
| 48 | + // RFC 1950 test vector |
| 49 | + uint8_t data[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 50 | + uint32_t sum = adler32(data, 9); |
| 51 | + REQUIRE(sum == 0x091e01de); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_CASE("Adler32 all zeros") { |
| 55 | + uint8_t data[10] = {}; |
| 56 | + uint32_t sum = adler32(data, 10); |
| 57 | + // Initial: a=1, b=0. Each zero byte: a stays 1, b += a = b+1. |
| 58 | + // After 10 zeros: a=1, b=10. |
| 59 | + REQUIRE(sum == ((10 << 16) | 1)); |
| 60 | +} |
| 61 | + |
| 62 | +// --- Chaining --- |
| 63 | + |
| 64 | +TEST_CASE("Adler32 chaining produces same result") { |
| 65 | + uint8_t data[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 66 | + uint32_t whole = adler32(data, 9); |
| 67 | + |
| 68 | + // Compute in two chunks |
| 69 | + uint32_t partial = adler32(data, 4); |
| 70 | + uint32_t chained = adler32(data + 4, 5, partial); |
| 71 | + REQUIRE(chained == whole); |
| 72 | +} |
| 73 | + |
| 74 | +TEST_CASE("Adler32 chaining three chunks") { |
| 75 | + uint8_t data[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 76 | + uint32_t whole = adler32(data, 9); |
| 77 | + |
| 78 | + uint32_t s1 = adler32(data, 3); |
| 79 | + uint32_t s2 = adler32(data + 3, 3, s1); |
| 80 | + uint32_t s3 = adler32(data + 6, 3, s2); |
| 81 | + REQUIRE(s3 == whole); |
| 82 | +} |
| 83 | + |
| 84 | +// --- Byte vs word consistency --- |
| 85 | + |
| 86 | +TEST_CASE("Adler32 byte and word variants match") { |
| 87 | + // Word-aligned data |
| 88 | + uint32_t words[] = {0x04030201, 0x08070605}; |
| 89 | + uint8_t *bytes = reinterpret_cast<uint8_t *>(words); |
| 90 | + |
| 91 | + uint32_t byte_sum = adler32_bytes(bytes, 8); |
| 92 | + uint32_t word_sum = adler32_words(words, 2); |
| 93 | + REQUIRE(byte_sum == word_sum); |
| 94 | +} |
| 95 | + |
| 96 | +// --- Larger buffer --- |
| 97 | + |
| 98 | +TEST_CASE("Adler32 sequential byte pattern") { |
| 99 | + uint8_t data[256]; |
| 100 | + for (int i = 0; i < 256; i++) data[i] = i; |
| 101 | + uint32_t sum = adler32(data, 256); |
| 102 | + // Just verify it's non-trivial and deterministic |
| 103 | + REQUIRE(sum != 0); |
| 104 | + REQUIRE(sum != 1); |
| 105 | + // Re-compute to verify determinism |
| 106 | + uint32_t sum2 = adler32(data, 256); |
| 107 | + REQUIRE(sum == sum2); |
| 108 | +} |
0 commit comments