From 05e47704a54d3d7660ee45f5cf9354bdc03db084 Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis <karakasis@cscs.ch> Date: Wed, 26 Oct 2016 18:09:31 +0200 Subject: [PATCH] Adds unit tests for the STATE block. --- data/test.mod | 4 ++-- modcc/module.cpp | 9 +++++++++ modcc/module.hpp | 1 + modcc/parser.hpp | 7 +++---- tests/modcc/test_parser.cpp | 38 ++++++++++++++++++++++++++++++++++++- 5 files changed, 52 insertions(+), 7 deletions(-) diff --git a/data/test.mod b/data/test.mod index 0f95e717..bbb8ce41 100644 --- a/data/test.mod +++ b/data/test.mod @@ -10,8 +10,8 @@ NEURON { } STATE { - h (mA) - m (mV) r (S) + h (nA) + m r } UNITS { diff --git a/modcc/module.cpp b/modcc/module.cpp index c9f749e9..509d7755 100644 --- a/modcc/module.cpp +++ b/modcc/module.cpp @@ -40,6 +40,15 @@ Module::Module(std::vector<char> const& buffer) buffer_.push_back(0); } +Module::Module(const char* buffer, size_t count) +{ + for (auto i = 0; i < count-1 && *buffer != '\0'; ++buffer) { + buffer_.push_back(*buffer); + } + + buffer_.push_back(0); +} + std::vector<Module::symbol_ptr>& Module::procedures() { return procedures_; diff --git a/modcc/module.hpp b/modcc/module.hpp index f427d870..1cd1cfe8 100644 --- a/modcc/module.hpp +++ b/modcc/module.hpp @@ -15,6 +15,7 @@ public : Module(std::string const& fname); Module(std::vector<char> const& buffer); + Module(const char* buffer, size_t count); std::vector<char> const& buffer() const { return buffer_; diff --git a/modcc/parser.hpp b/modcc/parser.hpp index 6768749d..b55bede1 100644 --- a/modcc/parser.hpp +++ b/modcc/parser.hpp @@ -39,9 +39,6 @@ public: return error_string_; } -private: - Module *module_; - // functions for parsing descriptive blocks // these are called in the first pass, and do not // construct any AST information @@ -52,6 +49,9 @@ private: void parse_assigned_block(); void parse_title(); +private: + Module *module_; + std::vector<Token> comma_separated_identifiers(); std::vector<Token> unit_description(); @@ -69,4 +69,3 @@ private: bool expect(tok, const char *str=""); bool expect(tok, std::string const& str); }; - diff --git a/tests/modcc/test_parser.cpp b/tests/modcc/test_parser.cpp index 2d09e570..1edfe1a9 100644 --- a/tests/modcc/test_parser.cpp +++ b/tests/modcc/test_parser.cpp @@ -15,7 +15,7 @@ TEST(Parser, full_file) { } TEST(Parser, procedure) { - std::vector< const char*> calls = + std::vector<const char*> calls = { "PROCEDURE foo(x, y) {" " LOCAL a\n" @@ -562,3 +562,39 @@ TEST(Parser, parse_binop) { std::cout << red("error") << p.error_message() << std::endl; } } + +TEST(Parser, parse_state_block) { + std::vector<const char*> state_blocks = { + "STATE {\n" + " h\n" + " m r\n" + "}", + "STATE {\n" + " h (nA)\n" + " m r\n" + "}", + "STATE {\n" + " h (nA)\n" + " m (nA) r\n" + "}", + "STATE {\n" + " h (nA)\n" + " m r (uA)\n" + "}", + "STATE {\n" + " h (nA)\n" + " m (nA) r (uA)\n" + "}" + }; + + for (auto const& str: state_blocks) { + Module m(str, sizeof(str)); + Parser p(m, false); + p.parse_state_block(); + EXPECT_EQ(lexerStatus::happy, p.status()); + if (p.status() == lexerStatus::error) { + std::cout << str << "\n" + << red("error") << p.error_message() << "\n"; + } + } +} -- GitLab