summaryrefslogtreecommitdiffstats
path: root/tests/testanyref.cc
blob: d2ac7fa3b979f32b7b297c82bc4d5f2abfbf4140 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       [email protected]

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#include "common.h"
#include "test.h"

using namespace Arts;
using namespace std;

struct TestAnyRef : public TestCase
{
	TESTCASE(TestAnyRef);

	Dispatcher dispatcher;
	Buffer *buffer, *buffer2;
	Any any;

	void setUp() {
		buffer = new Buffer();
		any.type = "";
		any.value.clear();
	}
	void tearDown() {
		delete buffer;		buffer = 0;
	}
	void copy(AnyRef& src, AnyRef& dest)
	{
		Buffer tmp;
		src.write(&tmp);
		tmp.rewind();
		dest.read(&tmp);
	}
	TEST(ioAnyFloat) {
		// create a buffer with a float and a end-of-buffer delimiter
		float f = 1.1;
		buffer->writeFloat(f);
		buffer->writeLong(12345678);
		buffer->rewind();

		// see if reading it as float reads the *only* the float
		any.type = "float";

		AnyRef ref(any);
		ref.read(buffer);
		testEquals(12345678,buffer->readLong());
		testEquals(4, any.value.size());

		// check if the value is all right by copying it to a "real" float
		float f2;
		AnyRef ref2(f2);
		copy(ref,ref2);
		testEquals(f,f2);
	}
	TEST(ioAnyType) {
		// do the same again with a complex structured type
		Arts::InterfaceDef objInterface =
			Dispatcher::the()->interfaceRepo().queryInterface("Arts::Object");
		testEquals("Arts::Object", objInterface.name);

		objInterface.writeType(*buffer);
		buffer->writeLong(12345678);
		buffer->rewind();

		vector<mcopbyte> objInterfaceRaw;
		buffer->read(objInterfaceRaw, buffer->remaining() - 4);
		buffer->rewind();
		testAssert(objInterfaceRaw.size() > 50);

		any.type = "Arts::InterfaceDef";
		AnyRef ref(any);
		ref.read(buffer);
		testEquals(12345678,buffer->readLong());
		testAssert(objInterfaceRaw == any.value);
	}

};

TESTMAIN(TestAnyRef);