Skip to content

Commit

Permalink
Replace all -1 denoting no size with INT_MIN
Browse files Browse the repository at this point in the history
This is because e.g. SIZE(N - 1) will evaluate to SIZE(-1) in spec checking,
which denotes no size, which is incorrect
  • Loading branch information
fushar committed Jan 29, 2017
1 parent 20ae23c commit 4fb8e15
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions include/tcframe/io_manipulator/LineIOSegmentManipulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions include/tcframe/io_manipulator/LinesIOSegmentManipulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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 + ")";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions include/tcframe/spec/io/GridIOSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
Expand Down
4 changes: 2 additions & 2 deletions include/tcframe/spec/io/IOFormat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
4 changes: 4 additions & 0 deletions include/tcframe/spec/io/IOSegment.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once

#include <climits>

namespace tcframe {

const int NO_SIZE = INT_MIN;

enum class IOSegmentType {
GRID,
LINE,
Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/spec/io/LineIOSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct LineIOSegmentVariable {

LineIOSegmentVariable(Variable* variable)
: variable_(variable)
, size_([] {return -1;}) {
, size_([] {return NO_SIZE;}) {
}

Variable* variable() const {
Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/spec/io/LinesIOSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion include/tcframe/spec/io/RawLinesIOSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions test/ete/resources/normal-complex-formats/spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -43,11 +43,10 @@ class TestSpec : public BaseTestSpec<ProblemSpec> {
void SampleTestCase1() {
Input({
"[BEGIN INPUT]",
"2",
"3",
"3 5",
"1 1",
"2 2",
"3 3",
"7 7 7",
"8 8 8"
});
Expand All @@ -59,6 +58,6 @@ class TestSpec : public BaseTestSpec<ProblemSpec> {
}

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} });
}
};

0 comments on commit 4fb8e15

Please sign in to comment.