aboutsummaryrefslogtreecommitdiff
path: root/c++-libbu++/src/unit/float.unit
blob: b1eb063e6a0a7b3dff43fbe1a9bfd5e906670a02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// vim: syntax=cpp
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "gats/dictionary.h"
#include "gats/float.h"
#include "gats/string.h"

#include "bu/membuf.h"
#include "bu/list.h"
#include "bu/sio.h"

#include <stdlib.h>
#include <math.h>

using namespace Bu;

suite Basic
{
	void rw( double dVal )
	{
		Bu::MemBuf mb;

		Gats::Float( dVal ).write( mb );

		mb.setPos( 0 );

		Gats::Object *pObj = Gats::Object::read( mb );
		unitTest( pObj != NULL );
		unitTest( pObj->getType() == Gats::typeFloat );
		Gats::Float *pFlt = dynamic_cast<Gats::Float *>(pObj);
		Bu::String sHex;
		for( Bu::String::iterator i = mb.getString().begin(); i; i++ )
		{
			sHex += "0123456789abcdef"[(((uint8_t)*i)>>4)&0x0f];
			sHex += "0123456789abcdef"[(*i)&0x0f];
			sHex += ' ';
		}
		printf("In:  %a\nOut: %a\nRaw: %s\n", dVal, pFlt->getValue(), sHex.getStr() );
		if( isnan( dVal ) )
			unitTest( isnan(pFlt->getValue()) == isnan(dVal) );
		else
			unitTest( pFlt->getValue() == dVal );
		unitTest( signbit(pFlt->getValue()) == signbit(dVal) );

		delete pObj;
	}

	test positive
	{
		rw( 8485738457.0 );
		rw( 63723.0 );
		rw( 0.000000000000001928173 );
		rw( 1.0 );
		rw( 0.0 );
		rw( M_PI );
	}

	test negitave
	{
		rw( -8485738457.0 );
		rw( -63723.0 );
		rw( -0.000000000000001928173 );
		rw( -1.0 );
		rw( -0.0 );
		rw( -M_PI );
	}

	test inf
	{
		rw( INFINITY );
		rw( -INFINITY );
	}

	test nan
	{
		rw( NAN );
		rw( -NAN );
	}
}