bf01490c369c0b637ef0641b5674cb9233ce0087
[synfig.git] / ETL / test / value.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Generic Value Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Adrian Bentley
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #include <ETL/value>
25 #include <stdio.h>
26
27 /* === M A C R O S ========================================================= */
28
29 using namespace etl;
30
31 /* === C L A S S E S ======================================================= */
32
33 /* === P R O C E D U R E S ================================================= */
34
35 /* === E N T R Y P O I N T ================================================= */
36
37 struct stupidv
38 {
39         float x,y;
40
41         stupidv(float xin=0, float yin=0) :x(xin),y(yin) {}
42         void print() const
43         {
44                 printf("(x=%f,y=%f)\n",x,y);
45         }
46 };
47
48 struct stupidp
49 {
50         float z,w;
51
52         stupidp(float zin=0, float win=0) :z(zin),w(win) {}
53
54         void print() const
55         {
56                 printf("(z=%f,w=%f)\n",z,w);
57         }
58 };
59
60 template <>
61 class etl::value_store_type<stupidp>
62 {
63         typedef stupidv value_type;
64 };
65
66 int main()
67 {
68         try
69         {
70         value   v(10.0); //construction
71         value   v2;              //default construct...
72
73         //get type...
74         printf("type of 10.0: %s\n", v.type().name());
75
76         v2 = 1; //assignment
77         printf("type of 1: %s\n", v2.type().name());
78
79         //extract int test
80
81         int *pi = value_cast<int>(&v2);
82         printf("v2 is an int(%p)\n", pi);
83         printf("        %d\n", value_cast<int>(v2));
84
85         printf("        const version: %d\n", value_cast<int>(value(5)));
86
87         v = 'c'; //assignment again...
88         printf("type of c: %s\n", v.type().name());
89
90         v2 = v; //value assignment
91         printf("type of v2 , v: %s , %s\n", v2.type().name(), v.type().name());
92
93         //random type test
94         v = stupidv(0,1);
95         printf("type of vec: %s\n", v.type().name());
96
97         //type cast with binary change test
98         value_cast<stupidp>(&v)->print();
99         value_cast<stupidv>(stupidp(5,10)).print(); //copy test
100
101         printf("type of v: %s\n", v.type().name());
102         printf("type of v2: %s\n", v2.type().name());
103         v.swap(v2);
104         printf("type of v: %s\n", v.type().name());
105         printf("type of v2: %s\n", v2.type().name());
106
107         // test the exception throwing...
108         value_cast<int>(stupidp(6,66));
109
110         }catch(const etl::bad_value_cast &e)
111         {
112                 printf("        Exploded: %s\n",e.what());
113         }catch(...)
114         {
115                 printf("        Exploded\n");
116         }
117
118         return 0;
119 }