/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int
pack3 (int a, int b, int c)
{
a = (a + 16) & 0x1F;
b = (b + 16) & 0x1F;
c = (c + 16) & 0x1F;
return (a << 10) | (b << 5) | c;
}
void
unpack3 (int p, int &a, int &b, int &c)
{
c = (p & 0x1f) - 16;
b = ((p >> 5) & 0x1f) - 16;
a = ((p >> 10) & 0x1f) - 16;
}
void
test ()
{
int pass=0;
int fail=0;
for (int i = 0; i < 100; ++i)
{
int a = (rand () % 32) - 16;
int b = (rand () % 32) - 16;
int c = (rand () % 32) - 16;
int p = pack3 (a, b, c);
std::cout << "Pack (" << a << "," << b << "," << c
<< ") value " << p << " ";
int a2, b2, c2;
unpack3 (p, a2, b2, c2);
if ((a != a2) || (b != b2) || (c != c2))
{
std::cout << "FAIL returned (" << a2 << "," << b2 << "," << c2 <<
")\n";
++fail;
}
else
{
std::cout << "Pass\n";
++pass;
}
}
if (0 != fail)
{
std::cout << "FAIL: " << fail << " fails " << pass << "passed.\n";
}
else
{
std::cout << "PASS: " << pass << " tests passed.\n";
}
}
int
main ()
{
cout << "Testing packing" << endl;
test ();
cout << "Done" << endl;
return 0;
}