Skip to content

Commit

Permalink
[#3] Added test for RawJPEGFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Clovel committed Jun 10, 2019
1 parent e9b22e9 commit d9039c2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
Binary file added ressources/lena30.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_test(test_base64_decode tests_${PROJECT_NAME} 2)
add_test(test_base64_encode tests_${PROJECT_NAME} 3)
add_test(test_literal_to_string tests_${PROJECT_NAME} 4)
add_test(test_string_to_literal tests_${PROJECT_NAME} 5)
add_test(test_raw_jpeg_factory tests_${PROJECT_NAME} 6)
# add_test(XXXXX tests_${PROJECT_NAME} 0)

add_executable(tests_${PROJECT_NAME}
Expand Down
81 changes: 76 additions & 5 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
#include "Base64Encoder.hpp"
#include "LiteralConverter.hpp"

#include "RawJPEGFactory.hpp"

/* C++ system */
#include <iostream>
#include <fstream>
#include <string>
#include <exception>

/* C system */
#include <cstring>
Expand Down Expand Up @@ -33,7 +37,7 @@ int test_base64_encode_decode(void) {

assert(lStr == lDecodedStr);

return 0;
return EXIT_SUCCESS;
}

int test_base64_decode_encode(void) {
Expand All @@ -49,7 +53,7 @@ int test_base64_decode_encode(void) {

assert(lStr == lEncodedStr);

return 0;
return EXIT_SUCCESS;
}

int test_base64_decode(void) {
Expand All @@ -63,7 +67,7 @@ int test_base64_decode(void) {

assert(lExpectedStr == lDecodedStr);

return 0;
return EXIT_SUCCESS;
}

int test_base64_encode(void) {
Expand All @@ -82,7 +86,7 @@ int test_base64_encode(void) {

assert(lExpectedStr == lEncodedStr);

return 0;
return EXIT_SUCCESS;
}

int test_literal_to_string(void) {
Expand All @@ -93,7 +97,7 @@ int test_literal_to_string(void) {

assert(lExpectedStr == lResult);

return 0;
return EXIT_SUCCESS;
}

int test_string_to_literal(void) {
Expand All @@ -107,6 +111,70 @@ int test_string_to_literal(void) {
return 0;
}

int test_raw_jpeg_factory(void) {
std::ifstream lInputFileStream;
lInputFileStream.open("../../ressources/lena30.jpg", std::ios_base::in | std::ios_base::binary | std::ios_base::ate);
if(lInputFileStream.fail() || !lInputFileStream.is_open()) {
std::cout << "[ERROR] Failed to open ../../ressources/lena30.jpg" << std::endl;
return EXIT_FAILURE;
}

std::string lRawJPEG;
size_t lStrRawLength = lInputFileStream.tellg();
std::cout << "[INFO ] lStrRawLength = " << lStrRawLength << std::endl;

/* Allocating memory for the string in advance */
try {
lRawJPEG.reserve(lStrRawLength);
} catch (const std::exception &e) {
std::cout << "[ERROR] Excpetion \"" << e.what() << "\" occured during allocation !" << std::endl;
return EXIT_FAILURE;
}
try {
lInputFileStream.seekg(0, std::ios::beg);
} catch (const std::bad_alloc &e) {
std::cout << "[ERROR] Excpetion \"" << e.what() << "\" occured during seekg back to BOF !" << std::endl;
return EXIT_FAILURE;
}

/* Getting the string */
try {
lRawJPEG.assign((std::istreambuf_iterator<char>(lInputFileStream)),
std::istreambuf_iterator<char>());
} catch (const std::exception &e) {
std::cout << "[ERROR] Excpetion \"" << e.what() << "\" occured during data read !" << std::endl;
return EXIT_FAILURE;
}

//std::cout << "[DEBUG] Raw contents are : " << lRawJPEG << std::endl;

/* Instanciate a RawJPEGFactory */
RawJPEGFactory lFactory;

/* Segmenting the string */
size_t lSegmentSize = lStrRawLength/100;
size_t lLeftSize = lStrRawLength % 100;
std::cout << "[DEBUG] lSegmentSize = " << lSegmentSize << ", lSegmentSize*100 = " << lSegmentSize*100 << ", left = " << lLeftSize << std::endl;

/* Getting segments and appending them to the Facotry string */
std::string lTmpStr, lAppendResult;
for(unsigned int i = 0U; 100 > i; ++i) {
lTmpStr = lRawJPEG.substr(i*lSegmentSize, lSegmentSize);
lAppendResult = lFactory.append(lTmpStr);
assert(std::string() == lAppendResult);
}
lTmpStr = lRawJPEG.substr(lStrRawLength - lLeftSize, lSegmentSize);
lAppendResult = lFactory.append(lTmpStr);

/* Check if the Factory returned the raw string */
assert(std::string() != lAppendResult);

/* Check if the Factory's result is equal to the original raw data */
assert(lRawJPEG == lAppendResult);

return EXIT_SUCCESS;
}


/* ----------------------------------------------------- */
/* main function --------------------------------------- */
Expand Down Expand Up @@ -147,6 +215,9 @@ int main(const int argc, const char * const * const argv)
case 5:
result = test_string_to_literal();
break;
case 6:
result = test_raw_jpeg_factory();
break;
default:
fprintf(stdout, "[INFO ] test #%d not available\n", test_num);
fflush(stdout);
Expand Down

0 comments on commit d9039c2

Please sign in to comment.