Source

PPMZ2 / UnitTests / UnitTests.h

Full commit
// UnitTests.h

#pragma once

#include <memory.h>
#include "ppmz2\Encoder.h"

using namespace System;
using namespace NUnit::Framework;
using namespace Ppmz2::Managed;

namespace UnitTests 
{

    [TestFixture]
	public ref class UnitTests
	{
    public:
        [Test]
        void TestManagedAPI()
        {
            System::String^ toEncode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            array<unsigned char>^ toEncodeBytes = System::Text::Encoding::ASCII->GetBytes(toEncode);
			array<unsigned char>^ encodedBytes = API::Encode(toEncodeBytes, CodingOptions::Default);
            array<unsigned char>^ decoded = API::Decode(encodedBytes, toEncodeBytes->Length, CodingOptions::Default);
            System::String^ decodedString = gcnew System::String(System::Text::Encoding::ASCII->GetChars(decoded));
            Assert::AreEqual(toEncode, decodedString);
        }

        [Test]
        void TestManagedAPIWithPrecondition()
        {
            System::String^ toEncode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            array<unsigned char>^ toEncodeBytes = System::Text::Encoding::ASCII->GetBytes(toEncode);
            array<unsigned char>^ encodedBytes = API::Encode(toEncodeBytes, toEncodeBytes, CodingOptions::Default);
            array<unsigned char>^ decoded = API::Decode(encodedBytes, toEncodeBytes->Length, toEncodeBytes, CodingOptions::Default);
            System::String^ decodedString = gcnew System::String(System::Text::Encoding::ASCII->GetChars(decoded));
            Assert::AreEqual(toEncode, decodedString);
        }


        [Test]
        void TestUnmanagedAPI()
        {
            char* toEncode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int length = strlen(toEncode) + 1;
            Ppmz2::Encoder encoder;
            unsigned char* compressedBuffer = NULL;
            unsigned int encodedLength = encoder.Encode((unsigned char*) toEncode, length, &compressedBuffer, Ppmz2::CodingOptions::Default);
            unsigned char* reconstructedBuffer = NULL;
            encoder.Decode(&reconstructedBuffer, length, compressedBuffer, Ppmz2::CodingOptions::Default);

            Assert::IsTrue(compressedBuffer != NULL);
            Assert::IsTrue(reconstructedBuffer != NULL);
            char* reconstructedString = (char*) reconstructedBuffer;
            for (int i = 0; i < length; ++i)
            {
                Assert::IsTrue(toEncode[i] == reconstructedBuffer[i], "Arrays differed at index: " + i + " Encoded: " + (char) toEncode[i] + " Reconstructed: " + (char) reconstructedBuffer[i]);
            }
            // TODO: why does this make it hang?
            //delete[] compressedBuffer;
            //delete[] reconstructedBuffer;
        }

        [Test]
        void TestUnmanagedAPIWithPrecondition()
        {
            char* toEncode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int length = strlen(toEncode) + 1;
            Ppmz2::Encoder encoder;
            unsigned char* compressedBuffer = NULL;
			unsigned int encodedLength = encoder.Encode((unsigned char*) toEncode, length, &compressedBuffer, (unsigned char*) toEncode, length, Ppmz2::CodingOptions::Default);
            unsigned char* reconstructedBuffer = NULL;
            encoder.Decode(&reconstructedBuffer, length, compressedBuffer, (unsigned char*) toEncode, length, Ppmz2::CodingOptions::Default);

            Assert::IsTrue(compressedBuffer != NULL);
            Assert::IsTrue(reconstructedBuffer != NULL);
            char* reconstructedString = (char*) reconstructedBuffer;
            for (int i = 0; i < length; ++i)
            {
                Assert::IsTrue(toEncode[i] == reconstructedBuffer[i], "Arrays differed at index: " + i + " Encoded: " + toEncode[i] + " Reconstructed: " + reconstructedBuffer[i]);
            }
            delete[] compressedBuffer;
            delete[] reconstructedBuffer;
        }    
	};
}