﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Run00.MsTest;
using Crypto = System.Security.Cryptography;

namespace BitFn.HashingLibrary.Tests.Algorithms.CryptographyWrapper.MD5
{
	[TestClass]
	[CategorizeByConventionClass(typeof(ComputeHash))]
	public class ComputeHash
	{
		[TestMethod]
		[CategorizeByConvention]
		public void WhenCertainValuesGiven_ShouldReachIntMaxValue()
		{
			// Arrange
			const int expected = int.MaxValue;

			var values = new byte[]
			{
				0x08, 0x45, 0x7F, 0xA5, 0x07, 0xD2, 0x79, 0x6A, 0x91, 0x40, 0xB8, 0x42, 0xC1, 0x51, 0xDB, 0x06, 0xCB, 0x92, 0x03,
				0xDA, 0x7D, 0x7A, 0x54, 0x28, 0x14, 0xCA, 0x3D, 0x93, 0xE2, 0x0C, 0xAF, 0xBC, 0x93, 0x59, 0x8A, 0x8A, 0x1F, 0xAD,
				0x67, 0x83, 0xDE, 0x62, 0x98, 0xA4, 0x95, 0x58, 0x8B, 0xD9, 0xAC, 0x31, 0xD8, 0xB7, 0xCE, 0x53, 0x76, 0x8D, 0xF0,
				0x57, 0x90, 0x3C, 0x95, 0xE5, 0x87
			};
			var algorithm = new HashingLibrary.Algorithms.CryptographyWrapper(Crypto.MD5.Create());

			// Act
			var actual = algorithm.ComputeHash(values);

			// Assert
			var ints = new int[128 / 8 / sizeof(int)];
			Buffer.BlockCopy(actual, 0, ints, 0, actual.Length);
			CollectionAssert.Contains(ints, expected);
		}

		[TestMethod]
		[CategorizeByConvention]
		public void WhenCertainValuesGiven_ShouldReachIntMinValue()
		{
			// Arrange
			const int expected = int.MinValue;

			var values = new byte[]
			{
				0x0D, 0x3A, 0x1B, 0xC7, 0xC4, 0x89, 0xA9, 0x32, 0xC8, 0x0D, 0x5B, 0x8D, 0x05, 0xA7, 0xC8, 0x5B, 0x10, 0x38, 0x1E,
				0x72, 0x23, 0x74, 0x2D, 0x51, 0x88, 0x9A, 0x96, 0xB1, 0x4B, 0x83, 0xF2, 0x40, 0x5B, 0xCC, 0x43, 0x43, 0x74, 0x80,
				0x9B, 0x88, 0xFF, 0x1B, 0x25, 0x44, 0x4E, 0x4D, 0xD7, 0x75, 0xD7, 0x26, 0xFD, 0x18, 0xB8, 0xB8, 0xEF, 0x98, 0x0C,
				0xC9, 0x3F, 0x29, 0xF2, 0xF7, 0xE7
			};
			var algorithm = new HashingLibrary.Algorithms.CryptographyWrapper(Crypto.MD5.Create());

			// Act
			var actual = algorithm.ComputeHash(values);

			// Assert
			var ints = new int[128 / 8 / sizeof(int)];
			Buffer.BlockCopy(actual, 0, ints, 0, actual.Length);
			CollectionAssert.Contains(ints, expected);
		}
	}
}
