This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed -p, -o and -i + finally added tests for them
- Loading branch information
Marcin Kurczewski
committed
Jun 20, 2015
1 parent
99f86a3
commit 7ef4b34
Showing
4 changed files
with
143 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "util.h" | ||
|
||
File::OffsetType computeAutoPosition( | ||
File::OffsetType fileSize, | ||
size_t crcSize, | ||
bool overwrite) | ||
{ | ||
auto totalSize = fileSize; | ||
if (!overwrite) | ||
totalSize += crcSize; | ||
|
||
File::OffsetType targetPosition = crcSize | ||
? totalSize - crcSize | ||
: totalSize; | ||
|
||
if (targetPosition < 0) | ||
{ | ||
targetPosition += overwrite | ||
? fileSize - crcSize | ||
: fileSize; | ||
} | ||
|
||
if (targetPosition < 0 || | ||
targetPosition + static_cast<File::OffsetType>(crcSize) > totalSize) | ||
{ | ||
throw std::invalid_argument( | ||
"Patch position is located outside available input"); | ||
} | ||
|
||
return targetPosition; | ||
} | ||
|
||
File::OffsetType shiftUserPosition( | ||
File::OffsetType userPosition, | ||
File::OffsetType fileSize, | ||
size_t crcSize, | ||
bool overwrite) | ||
{ | ||
auto targetPosition = userPosition; | ||
auto totalSize = fileSize; | ||
if (!overwrite) | ||
totalSize += crcSize; | ||
|
||
while (targetPosition < 0) | ||
targetPosition += fileSize; | ||
|
||
if (targetPosition < 0 || | ||
targetPosition + static_cast<File::OffsetType>(crcSize) > totalSize) | ||
{ | ||
throw std::invalid_argument( | ||
"Patch position is located outside available input"); | ||
} | ||
|
||
return targetPosition; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef CLI_UTIL_H | ||
#define CLI_UTIL_H | ||
#include "File/File.h" | ||
|
||
File::OffsetType computeAutoPosition( | ||
File::OffsetType fileSize, | ||
size_t crcSize, | ||
bool overwrite); | ||
|
||
File::OffsetType shiftUserPosition( | ||
File::OffsetType userPosition, | ||
File::OffsetType fileSize, | ||
size_t crcSize, | ||
bool overwrite); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "catch.hh" | ||
#include "util.h" | ||
|
||
TEST_CASE("Automatic patch insertion position works", "[pos]") | ||
{ | ||
REQUIRE(computeAutoPosition(0, 4, false) == 0); | ||
REQUIRE(computeAutoPosition(0, 2, false) == 0); | ||
REQUIRE(computeAutoPosition(2, 4, false) == 2); | ||
REQUIRE(computeAutoPosition(2, 2, false) == 2); | ||
} | ||
|
||
TEST_CASE("Automatic patch overwrite position works", "[pos]") | ||
{ | ||
REQUIRE_THROWS(computeAutoPosition(0, 4, true)); | ||
REQUIRE_THROWS(computeAutoPosition(2, 4, true)); | ||
REQUIRE(computeAutoPosition(4, 4, true) == 0); | ||
REQUIRE(computeAutoPosition(5, 4, true) == 1); | ||
} | ||
|
||
TEST_CASE("Manual patch insertion position works", "[pos]") | ||
{ | ||
REQUIRE(shiftUserPosition(0, 4, 4, false) == 0); | ||
REQUIRE(shiftUserPosition(1, 4, 4, false) == 1); | ||
REQUIRE(shiftUserPosition(2, 4, 4, false) == 2); | ||
REQUIRE(shiftUserPosition(3, 4, 4, false) == 3); | ||
REQUIRE(shiftUserPosition(4, 4, 4, false) == 4); | ||
REQUIRE_THROWS(shiftUserPosition(5, 4, 4, false)); | ||
} | ||
|
||
TEST_CASE("Manual patch overwrite position works", "[pos]") | ||
{ | ||
REQUIRE(shiftUserPosition(0, 4, 4, true) == 0); | ||
REQUIRE_THROWS(shiftUserPosition(1, 4, 4, true)); | ||
REQUIRE_THROWS(shiftUserPosition(2, 4, 4, true)); | ||
REQUIRE_THROWS(shiftUserPosition(3, 4, 4, true)); | ||
REQUIRE_THROWS(shiftUserPosition(4, 4, 4, true)); | ||
REQUIRE_THROWS(shiftUserPosition(5, 4, 4, true)); | ||
REQUIRE(shiftUserPosition(0, 4, 2, true) == 0); | ||
REQUIRE(shiftUserPosition(1, 4, 2, true) == 1); | ||
REQUIRE(shiftUserPosition(2, 4, 2, true) == 2); | ||
REQUIRE_THROWS(shiftUserPosition(3, 4, 2, true)); | ||
} | ||
|
||
TEST_CASE("Manual negative patch insertion position works", "[pos]") | ||
{ | ||
REQUIRE(shiftUserPosition(-0, 4, 4, false) == 0); | ||
REQUIRE(shiftUserPosition(-1, 4, 4, false) == 3); | ||
REQUIRE(shiftUserPosition(-2, 4, 4, false) == 2); | ||
REQUIRE(shiftUserPosition(-3, 4, 4, false) == 1); | ||
REQUIRE(shiftUserPosition(-4, 4, 4, false) == 0); | ||
REQUIRE(shiftUserPosition(-5, 4, 4, false) == 3); | ||
} | ||
|
||
TEST_CASE("Manual negative patch overwrite position works", "[pos]") | ||
{ | ||
REQUIRE(shiftUserPosition(-0, 4, 4, true) == 0); | ||
REQUIRE_THROWS(shiftUserPosition(-1, 4, 4, true)); | ||
REQUIRE_THROWS(shiftUserPosition(-2, 4, 4, true)); | ||
REQUIRE_THROWS(shiftUserPosition(-3, 4, 4, true)); | ||
REQUIRE(shiftUserPosition(-4, 4, 4, true) == 0); | ||
REQUIRE_THROWS(shiftUserPosition(-5, 4, 4, true)); | ||
} |