Home | History | Annotate | Download | only in tests
      1 /**
      2  * @file cached_value_tests.cpp
      3  * tests cached_value.h
      4  *
      5  * @remark Copyright 2005 OProfile authors
      6  * @remark Read the file COPYING
      7  *
      8  * @author John Levon
      9  */
     10 
     11 #include <cstdlib>
     12 #include <iostream>
     13 #include <string>
     14 
     15 #include "cached_value.h"
     16 
     17 using namespace std;
     18 
     19 namespace {
     20 
     21 
     22 bool check_throw(cached_value<bool> const & boolval)
     23 {
     24 	try {
     25 		bool foo = boolval.get();
     26 		foo = false;
     27 	} catch (op_fatal_error const & e) {
     28 		return true;
     29 	}
     30 	return false;
     31 }
     32 
     33 
     34 int check_cached(void)
     35 {
     36 	cached_value<bool> boolval;
     37 	cached_value<string> strval;
     38 
     39 	if (!check_throw(boolval)) {
     40 		cerr << "get() on no value didn't throw\n";
     41 		return EXIT_FAILURE;
     42 	}
     43 
     44 	if (boolval.reset(false) != false || boolval.get() != false) {
     45 		cerr << "reset() of cached value \"false\" didn't work\n";
     46 		return EXIT_FAILURE;
     47 	}
     48 
     49 	if (boolval.reset(true) != true || boolval.get() != true) {
     50 		cerr << "reset() of cached value \"true\" didn't work\n";
     51 		return EXIT_FAILURE;
     52 	}
     53 
     54 	if (strval.reset("foo") != "foo" || strval.get() != "foo") {
     55 		cerr << "reset() of cached value \"foo\" didn't work\n";
     56 		return EXIT_FAILURE;
     57 	}
     58 
     59 	if (strval.reset("") != "" || strval.get() != "") {
     60 		cerr << "reset() of cached value \"\" didn't work\n";
     61 		return EXIT_FAILURE;
     62 	}
     63 
     64 	return EXIT_SUCCESS;
     65 }
     66 
     67 };
     68 
     69 int main()
     70 {
     71 	try {
     72 		check_cached();
     73 	}
     74 	catch (...) {
     75 		cerr << "unknown exception\n";
     76 		return EXIT_FAILURE;
     77 	}
     78 
     79 	return EXIT_SUCCESS;
     80 }
     81