regression_matlib_switch.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2009 Carl Barron
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/lex_static_lexertl.hpp>
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11. #include <exception>
  12. #include "matlib_static_switch.h"
  13. #include "matlib.h"
  14. void test_matrix(std::vector<std::vector<double> > const& x)
  15. {
  16. BOOST_TEST(x.size() == 3);
  17. BOOST_TEST(x[0].size() == 2 && x[0][0] == 1 && x[0][1] == 2);
  18. BOOST_TEST(x[1].size() == 1 && x[1][0] == 3);
  19. BOOST_TEST(x[2].size() == 3 && x[2][0] == 4 && x[2][1] == 5 && x[2][2] == 6);
  20. }
  21. int main()
  22. {
  23. std::string input("[[1,2][3][4,5,6]]");
  24. std::vector<std::vector<double> > results;
  25. typedef std::string::iterator iter;
  26. typedef boost::spirit::lex::lexertl::static_actor_lexer<
  27. boost::spirit::lex::lexertl::token<iter>,
  28. boost::spirit::lex::lexertl::static_::lexer_matlib_switch
  29. > lexer_type;
  30. typedef matlib_tokens<lexer_type> matlib_type;
  31. matlib_type matrix(results);
  32. iter first = input.begin();
  33. try {
  34. BOOST_TEST(boost::spirit::lex::tokenize(first, input.end(), matrix));
  35. test_matrix(results);
  36. }
  37. catch (std::runtime_error const& e) {
  38. std::cerr << e.what() << '\n';
  39. BOOST_TEST(false);
  40. }
  41. return boost::report_errors();
  42. }