From 4fb8e15714af40d52d5a8addd06b5f620436134f Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Sun, 29 Jan 2017 11:14:13 +0000 Subject: [PATCH] Replace all -1 denoting no size with INT_MIN This is because e.g. SIZE(N - 1) will evaluate to SIZE(-1) in spec checking, which denotes no size, which is incorrect --- .../tcframe/io_manipulator/LineIOSegmentManipulator.hpp | 4 ++-- .../tcframe/io_manipulator/LinesIOSegmentManipulator.hpp | 6 +++--- .../io_manipulator/RawLinesIOSegmentManipulator.hpp | 4 ++-- include/tcframe/spec/io/GridIOSegment.hpp | 6 +++--- include/tcframe/spec/io/IOFormat.hpp | 4 ++-- include/tcframe/spec/io/IOSegment.hpp | 4 ++++ include/tcframe/spec/io/LineIOSegment.hpp | 2 +- include/tcframe/spec/io/LinesIOSegment.hpp | 2 +- include/tcframe/spec/io/RawLinesIOSegment.hpp | 2 +- test/ete/resources/normal-complex-formats/spec.cpp | 7 +++---- 10 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/tcframe/io_manipulator/LineIOSegmentManipulator.hpp b/include/tcframe/io_manipulator/LineIOSegmentManipulator.hpp index 155b25cf..395b98c8 100644 --- a/include/tcframe/io_manipulator/LineIOSegmentManipulator.hpp +++ b/include/tcframe/io_manipulator/LineIOSegmentManipulator.hpp @@ -62,7 +62,7 @@ class LineIOSegmentManipulator { private: static void checkVectorSize(Vector* vektor, int size) { - if (size != -1 && vektor->size() != size) { + if (size != NO_SIZE && vektor->size() != size) { throw runtime_error("Number of elements of vector " + TokenFormatter::formatVariable(vektor->name()) + " unsatisfied. Expected: " + StringUtils::toString(size) + ", actual: " + StringUtils::toString(vektor->size())); @@ -75,7 +75,7 @@ class LineIOSegmentManipulator { static void parseVector(Vector* vektor, int size, istream* in) { vektor->clear(); - if (size == -1) { + if (size == NO_SIZE) { vektor->parseFrom(in); } else { vektor->parseFrom(in, size); diff --git a/include/tcframe/io_manipulator/LinesIOSegmentManipulator.hpp b/include/tcframe/io_manipulator/LinesIOSegmentManipulator.hpp index 2810fa4b..c2681dea 100644 --- a/include/tcframe/io_manipulator/LinesIOSegmentManipulator.hpp +++ b/include/tcframe/io_manipulator/LinesIOSegmentManipulator.hpp @@ -29,7 +29,7 @@ class LinesIOSegmentManipulator { int size = segment->size()(); for (int j = 0; j != size; j++) { - if (size == -1 && WhitespaceManipulator::isEof(in)) { + if (size == NO_SIZE && WhitespaceManipulator::isEof(in)) { break; } @@ -85,7 +85,7 @@ class LinesIOSegmentManipulator { private: static int getSize(LinesIOSegment* segment) { int size = segment->size()(); - if (size != -1) { + if (size != NO_SIZE) { return size; } @@ -112,7 +112,7 @@ class LinesIOSegmentManipulator { } if (size != expectedSize) { string withoutSizeMessage; - if (segment->size()() == -1) { + if (segment->size()() == NO_SIZE) { string firstVariableName = TokenFormatter::formatVariable(segment->variables()[0]->name()); withoutSizeMessage = " (number of elements of " + firstVariableName + ")"; } diff --git a/include/tcframe/io_manipulator/RawLinesIOSegmentManipulator.hpp b/include/tcframe/io_manipulator/RawLinesIOSegmentManipulator.hpp index 1ea5d350..cf1dc641 100644 --- a/include/tcframe/io_manipulator/RawLinesIOSegmentManipulator.hpp +++ b/include/tcframe/io_manipulator/RawLinesIOSegmentManipulator.hpp @@ -22,7 +22,7 @@ class RawLinesIOSegmentManipulator { Vector* variable = segment->variable(); int size = segment->size()(); for (int j = 0; j != size; j++) { - if (size == -1 && WhitespaceManipulator::isEof(in)) { + if (size == NO_SIZE && WhitespaceManipulator::isEof(in)) { break; } @@ -48,7 +48,7 @@ class RawLinesIOSegmentManipulator { static void checkVectorSize(RawLinesIOSegment* segment) { Vector* variable = segment->variable(); int expectedSize = segment->size()(); - if (expectedSize != -1 && expectedSize != variable->size()) { + if (expectedSize != NO_SIZE && expectedSize != variable->size()) { throw runtime_error( "Number of elements of " + TokenFormatter::formatVariable(variable->name()) + " unsatisfied. Expected: " + StringUtils::toString(expectedSize) diff --git a/include/tcframe/spec/io/GridIOSegment.hpp b/include/tcframe/spec/io/GridIOSegment.hpp index ab3c1116..5fca3477 100644 --- a/include/tcframe/spec/io/GridIOSegment.hpp +++ b/include/tcframe/spec/io/GridIOSegment.hpp @@ -25,8 +25,8 @@ struct GridIOSegment : public IOSegment { public: GridIOSegment() : variable_(nullptr) - , rows_([] {return -1;}) - , columns_([] {return -1;}) {} + , rows_([] {return NO_SIZE;}) + , columns_([] {return NO_SIZE;}) {} IOSegmentType type() const { return IOSegmentType::GRID; @@ -97,7 +97,7 @@ class GridIOSegmentBuilder : public IOSegmentBuilder { if (subject_->variable_ == nullptr) { throw runtime_error("Grid segment must have exactly one variable"); } - if (subject_->rows_() == -1 || subject_->columns_() == -1) { + if (subject_->rows_() == NO_SIZE || subject_->columns_() == NO_SIZE) { throw runtime_error("Grid segment must define matrix sizes"); } } diff --git a/include/tcframe/spec/io/IOFormat.hpp b/include/tcframe/spec/io/IOFormat.hpp index 99fb5ac4..48b5efae 100644 --- a/include/tcframe/spec/io/IOFormat.hpp +++ b/include/tcframe/spec/io/IOFormat.hpp @@ -141,14 +141,14 @@ class IOFormatBuilder { if (segment->type() != IOSegmentType::LINES) { return false; } - return ((LinesIOSegment*) segment)->size()() == -1; + return ((LinesIOSegment*) segment)->size()() == NO_SIZE; } bool isRawLinesSegmentWithoutSize(IOSegment* segment) { if (segment->type() != IOSegmentType::RAW_LINES) { return false; } - return ((RawLinesIOSegment*) segment)->size()() == -1; + return ((RawLinesIOSegment*) segment)->size()() == NO_SIZE; } }; diff --git a/include/tcframe/spec/io/IOSegment.hpp b/include/tcframe/spec/io/IOSegment.hpp index 457f5fe7..888ba7d1 100644 --- a/include/tcframe/spec/io/IOSegment.hpp +++ b/include/tcframe/spec/io/IOSegment.hpp @@ -1,7 +1,11 @@ #pragma once +#include + namespace tcframe { +const int NO_SIZE = INT_MIN; + enum class IOSegmentType { GRID, LINE, diff --git a/include/tcframe/spec/io/LineIOSegment.hpp b/include/tcframe/spec/io/LineIOSegment.hpp index 7671f152..0e1523b2 100644 --- a/include/tcframe/spec/io/LineIOSegment.hpp +++ b/include/tcframe/spec/io/LineIOSegment.hpp @@ -27,7 +27,7 @@ struct LineIOSegmentVariable { LineIOSegmentVariable(Variable* variable) : variable_(variable) - , size_([] {return -1;}) { + , size_([] {return NO_SIZE;}) { } Variable* variable() const { diff --git a/include/tcframe/spec/io/LinesIOSegment.hpp b/include/tcframe/spec/io/LinesIOSegment.hpp index 97259508..c016997f 100644 --- a/include/tcframe/spec/io/LinesIOSegment.hpp +++ b/include/tcframe/spec/io/LinesIOSegment.hpp @@ -62,7 +62,7 @@ class LinesIOSegmentBuilder : public IOSegmentBuilder { public: LinesIOSegmentBuilder() : subject_(new LinesIOSegment()) { - subject_->size_ = [] {return -1;}; + subject_->size_ = [] {return NO_SIZE;}; } LinesIOSegmentBuilder& addVectorVariable(Vector* variable) { diff --git a/include/tcframe/spec/io/RawLinesIOSegment.hpp b/include/tcframe/spec/io/RawLinesIOSegment.hpp index b1a4944b..42893e0b 100644 --- a/include/tcframe/spec/io/RawLinesIOSegment.hpp +++ b/include/tcframe/spec/io/RawLinesIOSegment.hpp @@ -51,7 +51,7 @@ class RawLinesIOSegmentBuilder : public IOSegmentBuilder { public: RawLinesIOSegmentBuilder() : subject_(new RawLinesIOSegment()) { - subject_->size_ = [] {return -1;}; + subject_->size_ = [] {return NO_SIZE;}; } RawLinesIOSegmentBuilder& addVectorVariable(Vector* variable) { diff --git a/test/ete/resources/normal-complex-formats/spec.cpp b/test/ete/resources/normal-complex-formats/spec.cpp index e23ff70d..3b0b2b51 100644 --- a/test/ete/resources/normal-complex-formats/spec.cpp +++ b/test/ete/resources/normal-complex-formats/spec.cpp @@ -19,7 +19,7 @@ class ProblemSpec : public BaseProblemSpec { RAW_LINE(S); LINE(N); LINE(A % SIZE(2)); - LINES(X, Y) % SIZE(N + 1); + LINES(X, Y) % SIZE(N - 1); GRID(M) % SIZE(2, 3); } @@ -43,11 +43,10 @@ class TestSpec : public BaseTestSpec { void SampleTestCase1() { Input({ "[BEGIN INPUT]", - "2", + "3", "3 5", "1 1", "2 2", - "3 3", "7 7 7", "8 8 8" }); @@ -59,6 +58,6 @@ class TestSpec : public BaseTestSpec { } void TestCases() { - CASE(S = "[BEGIN INPUT]", N = 2, A = {10, 20}, X = {0, 0, 0}, Y = {1, 1, 1}, M = { {1, 2, 3}, {4, 5, 6} }); + CASE(S = "[BEGIN INPUT]", N = 3, A = {10, 20}, X = {0, 0}, Y = {1, 1}, M = { {1, 2, 3}, {4, 5, 6} }); } };