oct 0.25.0

Octonary transcodings.
Documentation
// Copyright 2024-2025 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.

#![cfg(test)]

use core::char;

macro_rules! test {
	{$( $ty:ty { $($lhs:expr => $rhs:expr),+$(,)? }$(,)?)*} => {
		$($({
			let mut data: &[u8] = &$lhs;

			let lhs = <$ty as ::oct::io::Deserialise>::deserialise(&mut data).unwrap();
			let rhs = $rhs;

			::core::assert_eq!(lhs, rhs);
		})*)*
	};
}

#[test]
fn test_deserialise() {
	test! {
		i8 {
			[0x00] =>  0x00,
			[0x7F] =>  0x7F,
			[0x80] => -0x80,
			[0xFF] => -0x01,
		}

		i16 {
			[0x00, 0x00] =>  0x0000,
			[0xFF, 0x7F] =>  0x7FFF,
			[0x00, 0x80] => -0x8000,
			[0xFF, 0xFF] => -0x0001,
		}

		i32 {
			[0x00, 0x00, 0x00, 0x00] =>  0x00000000,
			[0xFF, 0xFF, 0xFF, 0x7F] =>  0x7FFFFFFF,
			[0x00, 0x00, 0x00, 0x80] => -0x80000000,
			[0xFF, 0xFF, 0xFF, 0xFF] => -0x00000001,
		}

		i64 {
			[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] =>  0x0000000000000000,
			[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F] =>  0x7FFFFFFFFFFFFFFF,
			[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80] => -0x8000000000000000,
			[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF] => -0x0000000000000001,
		}

		u128 {
			[
				0x7F, 0x8F, 0x6F, 0x9F, 0x5F, 0xAF, 0x4F, 0xBF,
				0x3F, 0xCF, 0x2F, 0xDF, 0x1F, 0xEF, 0x0F, 0xFF,
			] => 0xFF_0F_EF_1F_DF_2F_CF_3F_BF_4F_AF_5F_9F_6F_8F_7F,
		}

		char {
			[0xFD, 0xFF, 0x00, 0x00] => char::REPLACEMENT_CHARACTER,
		}

		[char; 0x5] {
			[
				0xBB, 0x03, 0x00, 0x00, 0x91, 0x03, 0x00, 0x00,
				0xBC, 0x03, 0x00, 0x00, 0x94, 0x03, 0x00, 0x00,
				0xB1, 0x03, 0x00, 0x00,
			] => ['\u{03BB}', '\u{0391}', '\u{03BC}', '\u{0394}', '\u{03B1}'],
		}

		Option<()> {
			[0x00] => None,
			[0x01] => Some(()),
		}

		Result<(), i8> {
			[0x00, 0x00] => Ok(()),
			[0x01, 0x7F] => Err(i8::MAX),
		}
	}
}