Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65730 - in sandbox/opaque/libs/opaque: example test
From: vicente.botet_at_[hidden]
Date: 2010-10-02 16:57:07


Author: viboes
Date: 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
New Revision: 65730
URL: http://svn.boost.org/trac/boost/changeset/65730

Log:
Opaque:
* Add examples

Added:
   sandbox/opaque/libs/opaque/example/ex2.cpp (contents, props changed)
   sandbox/opaque/libs/opaque/example/ex3.cpp (contents, props changed)
   sandbox/opaque/libs/opaque/example/ex4.cpp (contents, props changed)
   sandbox/opaque/libs/opaque/example/ex5.cpp (contents, props changed)
   sandbox/opaque/libs/opaque/example/ex6.cpp (contents, props changed)
Text files modified:
   sandbox/opaque/libs/opaque/example/ex1.cpp | 6 ++----
   sandbox/opaque/libs/opaque/test/Jamfile.v2 | 5 +++++
   sandbox/opaque/libs/opaque/test/new_class_test.cpp | 17 ++++++++---------
   3 files changed, 15 insertions(+), 13 deletions(-)

Modified: sandbox/opaque/libs/opaque/example/ex1.cpp
==============================================================================
--- sandbox/opaque/libs/opaque/example/ex1.cpp (original)
+++ sandbox/opaque/libs/opaque/example/ex1.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -20,11 +20,10 @@
 
 bool gt(Int a, Int b)
 {
- if(a) return a > b; // 1
- else return 0; // 2
+ if(a) return a > b; // 1 don't fails even if a is not a bool
+ else return 0; // 2 don't fails even if 0 is not a bool
 }
 #else
-//~ BOOST_OPAQUE_PUBLIC_TYPEDEF(bool, Bool);
 struct private_unsigned_tag;
 typedef boost::new_type<unsigned, private_unsigned_tag, boost::mpl::vector<
     opaque::using_totally_ordered1<opaque::boolean>
@@ -34,7 +33,6 @@
 opaque::boolean gt(Int a, Int b)
 {
     if(a!=Int(0)) return a > b; // 1
- //~ else return false; // 2
     else return opaque::false_; // 2
 }
 #endif

Added: sandbox/opaque/libs/opaque/example/ex2.cpp
==============================================================================
--- (empty file)
+++ sandbox/opaque/libs/opaque/example/ex2.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -0,0 +1,73 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2010. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/opaque for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <boost/opaque/opaque.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace boost;
+using namespace boost::unit_test;
+
+
+BOOST_OPAQUE_PRIVATE_TYPEDEF(unsigned,game_score);
+
+game_score accumulate( game_score g1, game_score g2 ) {
+ return g1 + g2;
+}
+
+BOOST_OPAQUE_PUBLIC_TYPEDEF(unsigned,serial_number);
+
+
+serial_number next_id( serial_number n ) {
+ return n + serial_number(1u);
+}
+
+void accumulate_test() {
+ game_score gs1(1), gs2(2), res;
+ res= accumulate(gs1,gs2);
+ BOOST_CHECK(gs1<gs2);
+ BOOST_CHECK(gs1<=gs2);
+ BOOST_CHECK(gs2>=gs1);
+ BOOST_CHECK(res==game_score(3));
+ BOOST_CHECK(game_score(3)==res);
+
+ game_score res2;
+ res2=res+res;
+ BOOST_CHECK(res+res==game_score(6));
+}
+
+void next_id_test() {
+ serial_number sn(1), res;
+ res= next_id(sn);
+ BOOST_CHECK(res==serial_number(2));
+ BOOST_CHECK(serial_number(2)==res);
+
+ BOOST_CHECK(res+res==serial_number(4));
+
+ BOOST_CHECK(sn<res);
+}
+
+//~ void mix_test_fail() {
+ //~ game_score sn(1), res;
+ //~ res= next_id(sn);
+ //~ BOOST_CHECK(serial_number(3)==game_score(3));
+//~ }
+
+
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* test = BOOST_TEST_SUITE("ex2");
+ test->add(BOOST_TEST_CASE(&accumulate_test));
+ test->add(BOOST_TEST_CASE(&next_id_test));
+
+ return test;
+}
+
+

Added: sandbox/opaque/libs/opaque/example/ex3.cpp
==============================================================================
--- (empty file)
+++ sandbox/opaque/libs/opaque/example/ex3.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -0,0 +1,44 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2010. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/opaque for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <boost/opaque/opaque.hpp>
+
+#include <boost/test/unit_test.hpp>
+
+using namespace boost;
+using namespace boost::unit_test;
+
+// Listing 7
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double,mass1_leng2_per_time2);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(mass1_leng2_per_time2,energy);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(energy,kinetic_energy);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(energy,potential_energy);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(energy,heat_energy);
+
+void public_multiple_levels_test() {
+ energy e;
+ potential_energy p, q;
+
+ p = p + q; // ok
+ e = e + q; // ok
+ e = p + e; // ok
+}
+
+
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* test = BOOST_TEST_SUITE("ex3");
+ test->add(BOOST_TEST_CASE(&public_multiple_levels_test));
+
+
+ return test;
+}
+
+

Added: sandbox/opaque/libs/opaque/example/ex4.cpp
==============================================================================
--- (empty file)
+++ sandbox/opaque/libs/opaque/example/ex4.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -0,0 +1,47 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2010. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/opaque for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <boost/opaque/opaque.hpp>
+#include <boost/test/unit_test.hpp>
+
+using namespace boost;
+using namespace boost::unit_test;
+
+// Listing 1
+// Cartesian 3D coordinate types
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double, X);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double, Y);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double, Z);
+
+// polar 3D coordinate types
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double, Rho);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double, Theta);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(double, Phi);
+
+class PhysicsVector {
+public:
+ PhysicsVector(X, Y, Z);
+ PhysicsVector(Rho, Theta, Phi);
+ //...
+}; // PhysicsVector
+
+void test() {
+}
+
+
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* tests = BOOST_TEST_SUITE("ex4");
+ tests->add(BOOST_TEST_CASE(&test));
+
+ return tests;
+}
+
+

Added: sandbox/opaque/libs/opaque/example/ex5.cpp
==============================================================================
--- (empty file)
+++ sandbox/opaque/libs/opaque/example/ex5.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -0,0 +1,24 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2010. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/opaque for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+
+// Listing 2
+struct A { };
+struct B { B(A); };
+struct C { C(B); };
+
+void test() {
+ A a;
+ B ba = a; // okay: is_subst(A,B)
+ C cb = ba; // okay: is_subst(B,C)
+ C ca = a; // oops: ! is_subst(A,C) per [ISO03, §13.3.3.1.2/1]
+ // error: conversion from `A' to non-scalar type `C' requested
+}
+

Added: sandbox/opaque/libs/opaque/example/ex6.cpp
==============================================================================
--- (empty file)
+++ sandbox/opaque/libs/opaque/example/ex6.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -0,0 +1,24 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2010. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/opaque for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include <boost/opaque/opaque.hpp>
+
+
+// Listing 3
+struct C {};
+BOOST_OPAQUE_PUBLIC_TYPEDEF(C, B);
+BOOST_OPAQUE_PUBLIC_TYPEDEF(B, A);
+
+void test() {
+ A a;
+ B ba = a; // okay: is_subst(A,B)
+ C cb = ba; // okay: is_subst(B,C)
+ C ca = a; // okay: is_subst(A,C)
+}

Modified: sandbox/opaque/libs/opaque/test/Jamfile.v2
==============================================================================
--- sandbox/opaque/libs/opaque/test/Jamfile.v2 (original)
+++ sandbox/opaque/libs/opaque/test/Jamfile.v2 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -71,4 +71,9 @@
    test-suite "examples"
         :
         [ run ../example/ex1.cpp ]
+ [ run ../example/ex2.cpp ]
+ [ run ../example/ex3.cpp ]
+ [ run ../example/ex4.cpp ]
+ [ compile-fail ../example/ex5.cpp ]
+ [ compile ../example/ex6.cpp ]
         ;

Modified: sandbox/opaque/libs/opaque/test/new_class_test.cpp
==============================================================================
--- sandbox/opaque/libs/opaque/test/new_class_test.cpp (original)
+++ sandbox/opaque/libs/opaque/test/new_class_test.cpp 2010-10-02 16:57:05 EDT (Sat, 02 Oct 2010)
@@ -17,17 +17,16 @@
 
 
 struct private_unsigned :
- boost::new_class<private_unsigned, unsigned, boost::mpl::vector<opaque::using_equality_comparable1<> > >
-//~ , opaque::equality_comparable1<private_unsigned
- //~ >
- //~ >
+ boost::new_class<private_unsigned, unsigned
+ , boost::mpl::vector<opaque::using_equality_comparable1<>
+ >
+ >
 {
     typedef
- //~ boost::new_class<private_unsigned, unsigned
- //~ , opaque::equality_comparable1<private_unsigned
- //~ >
- //~ >
- boost::new_class<private_unsigned, unsigned, boost::mpl::vector<opaque::using_equality_comparable1<> > >
+ boost::new_class<private_unsigned, unsigned
+ , boost::mpl::vector<opaque::using_equality_comparable1<>
+ >
+ >
     base_type;
     
     private_unsigned(){}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk