📄 memorystream_test.cpp
字号:
//===========================================================================//
// File: MemoryStream_Test.cpp //
// Contents: Test stuff for MMIO class //
//---------------------------------------------------------------------------//
// Copyright (C) Microsoft Corporation. All rights reserved. //
//===========================================================================//
#include "StuffHeaders.hpp"
#include <ToolOS.hpp>
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MMIOstream ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool SingeBitStreamTest(int total_sections_to_write);
bool MultipleBitStreamTest(int total_sections_to_write);
bool FloatIntBitStreamTest(int total_sections_to_write);
//
//#############################################################################
//#############################################################################
//
bool
MemoryStream::TestClass()
{
SPEW((GROUP_STUFF_TEST, "Starting MemoryStream Single BitPacking Test..."));
Test_Assumption(SingeBitStreamTest(100));
SPEW((GROUP_STUFF_TEST, "Complete MemoryStream Single BitPacking Test!"));
SPEW((GROUP_STUFF_TEST, "Starting MemoryStream MultipleBitPacking Test..."));
//Test_Assumption(MultipleBitStreamTest(100));
SPEW((GROUP_STUFF_TEST, "Complete MemoryStream MultipleBitPacking Test!"));
SPEW((GROUP_STUFF_TEST, "Starting MemoryStream Int/Float BitPacking Test..."));
Test_Assumption(FloatIntBitStreamTest(1000));
SPEW((GROUP_STUFF_TEST, "Complete MemoryStream Int/Float BitPacking Test!"));
return true;
};
//
//#############################################################################
bool SingeBitStreamTest(int total_sections_to_write)
{
bool *source_array_of_bools = new bool[total_sections_to_write];
Register_Pointer(source_array_of_bools);
bool *comp_array_of_bools = new bool[total_sections_to_write];
Register_Pointer(comp_array_of_bools);
for (int i = 0; i < total_sections_to_write; ++i)
{
int random = Random::GetLessThan(10);
if (random > 4)
{
source_array_of_bools[i] = true;
}
else
{
source_array_of_bools[i] = false;
}
}
// calculate total byte depth.
int total_number_of_bytes = (int)(total_sections_to_write/8.0f);
int total_remainder_bits = total_sections_to_write - (total_number_of_bytes*8);
if ( total_remainder_bits != 0)
{
total_number_of_bytes += 1;
}
BYTE *big_byte_array = new BYTE[total_number_of_bytes];
Register_Pointer(big_byte_array);
MemoryStream bit_stream(big_byte_array, total_number_of_bytes);
for (i = 0; i < total_sections_to_write; ++i)
{
bit_stream.WriteBit(source_array_of_bools[i]);
}
bit_stream.ByteAlign();
bit_stream.Rewind();
for (i = 0; i < total_sections_to_write; ++i)
{
bit_stream.ReadBit(comp_array_of_bools[i]);
}
for (i = 0; i < total_sections_to_write; ++i)
{
Verify(source_array_of_bools[i] == comp_array_of_bools[i]);
Test_Assumption(source_array_of_bools[i] == comp_array_of_bools[i]);
}
Unregister_Pointer(big_byte_array);
delete[] big_byte_array;
Unregister_Pointer(source_array_of_bools);
delete[] source_array_of_bools;
Unregister_Pointer(comp_array_of_bools);
delete[] comp_array_of_bools;
return true;
}
//
//#############################################################################
template <class T> class MinMaxHolderOf
{
public:
T
minValue;
T
maxValue;
T
value;
MinMaxHolderOf()
{
}
};
MinMaxHolderOf<int>::MinMaxHolderOf()
{
minValue = -Random::GetInt();
maxValue = Random::GetInt();
int ramp = Random::GetLessThan(10)+1;
ramp *= ramp;
minValue *= ramp;
maxValue *= ramp;
if (minValue >= maxValue)
{
SPEW((GROUP_STUFF_TEST, "Should never reach here"));
minValue -= 10;
}
value = (int)((Random::GetFraction() * (maxValue - minValue))+minValue);
}
//#############################################################################
MinMaxHolderOf<float>::MinMaxHolderOf()
{
minValue = -Random::GetFraction();
maxValue = Random::GetFraction();
value = (Random::GetFraction() * (maxValue - minValue))+minValue;
}
//#############################################################################
bool FloatIntBitStreamTest(int total_sections_to_write)
{
// just to make this test interesting I will misalign the stream by one byte every number
int total_bits = 0;
bool *source_array_of_bools = new bool[total_sections_to_write];
Register_Pointer(source_array_of_bools);
bool *comp_array_of_bools = new bool[total_sections_to_write];
Register_Pointer(comp_array_of_bools);
int *convert_int_array = new int[total_sections_to_write];
Register_Pointer(convert_int_array);
float *convert_float_array = new float[total_sections_to_write];
Register_Pointer(convert_float_array);
int *comp_int_array = new int[total_sections_to_write];
Register_Pointer(comp_int_array);
float *comp_float_array = new float[total_sections_to_write];
Register_Pointer(comp_float_array);
int *float_bit_depth = new int[total_sections_to_write];
Register_Pointer(float_bit_depth);
int *int_bit_depth = new int[total_sections_to_write];
Register_Pointer(int_bit_depth);
MinMaxHolderOf<int> *int_min_max = new MinMaxHolderOf<int>[total_sections_to_write];
Register_Pointer(int_min_max);
MinMaxHolderOf<float> *float_min_max = new MinMaxHolderOf<float>[total_sections_to_write];
Register_Pointer(float_min_max);
for (int i = 0; i < total_sections_to_write; ++i)
{
int random = Random::GetLessThan(10);
if (random > 4)
{
source_array_of_bools[i] = true;
}
else
{
source_array_of_bools[i] = false;
}
float_bit_depth[i] = Random::GetLessThan(32) + 1;
int_bit_depth[i] = Random::GetLessThan(32) + 1;
Test_Assumption(float_bit_depth[i] > 0);
Test_Assumption(int_bit_depth[i] > 0);
Test_Assumption(float_bit_depth[i] <= 32);
Test_Assumption(int_bit_depth[i] <= 32);
total_bits += float_bit_depth[i];
total_bits += int_bit_depth[i];
}
// add in the bools
total_bits += total_sections_to_write;
// calculate total byte depth.
int total_number_of_bytes = (int)(total_bits/8.0f);
int total_remainder_bits = total_bits - (total_number_of_bytes*8);
if ( total_remainder_bits != 0)
{
total_number_of_bytes += 1;
}
BYTE *big_byte_array = new BYTE[total_number_of_bytes];
Register_Pointer(big_byte_array);
MemoryStream bit_stream(big_byte_array, total_number_of_bytes);
for (i = 0; i < total_sections_to_write; ++i)
{
//write int
bit_stream.WriteScaledIntToBits(int_min_max[i].value, int_min_max[i].minValue, int_min_max[i].maxValue, int_bit_depth[i]);
//write float
bit_stream.WriteScaledFloatToBits(float_min_max[i].value, float_min_max[i].minValue, float_min_max[i].maxValue, float_bit_depth[i]);
//write bit
bit_stream.WriteBit(source_array_of_bools[i]);
}
bit_stream.ByteAlign();
bit_stream.Rewind();
for (i = 0; i < total_sections_to_write; ++i)
{
//read int
bit_stream.ReadBitsToScaledInt(comp_int_array[i], int_min_max[i].minValue, int_min_max[i].maxValue, int_bit_depth[i]);
//read float
bit_stream.ReadBitsToScaledFloat(comp_float_array[i], float_min_max[i].minValue, float_min_max[i].maxValue, float_bit_depth[i]);
//read bit
bit_stream.ReadBit(comp_array_of_bools[i]);
}
SPEW((GROUP_STUFF_TEST, "-------------------"));
// convert the floats and bits and see if they match..
for (i = 0; i < total_sections_to_write; ++i)
{
DWORD buffer;
buffer = 0x00;
buffer = Scaled_Int_To_Bits(int_min_max[i].value, int_min_max[i].minValue, int_min_max[i].maxValue, int_bit_depth[i]);
convert_int_array[i] = Scaled_Int_From_Bits(buffer, int_min_max[i].minValue, int_min_max[i].maxValue, int_bit_depth[i]);;
buffer = 0x00;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -