From 1127ffa916abb945814fa13b75adec453fdf3b23 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 11 Dec 2021 12:47:32 +0300 Subject: [PATCH] Added encoded input size calculation --- QRCodeLibrary/Encode.cpp | 29 +++++++++++++++++++++ QRCodeLibrary/Encode.hpp | 14 ++++++++++ QRCodeLibrary/QRCodeLibrary.vcxproj | 2 ++ QRCodeLibrary/QRCodeLibrary.vcxproj.filters | 6 +++++ tests/Encode_test.cpp | 21 +++++++++++++++ tests/tests.vcxproj | 1 + 6 files changed, 73 insertions(+) create mode 100644 QRCodeLibrary/Encode.cpp create mode 100644 QRCodeLibrary/Encode.hpp create mode 100644 tests/Encode_test.cpp diff --git a/QRCodeLibrary/Encode.cpp b/QRCodeLibrary/Encode.cpp new file mode 100644 index 0000000..19e24fd --- /dev/null +++ b/QRCodeLibrary/Encode.cpp @@ -0,0 +1,29 @@ +#include "pch.h" + +#include "Encode.hpp" + +unsigned Encode::calculate_encoded_input_size(unsigned input_size, QRCodeMethod method) +{ + unsigned bit_num = 0; + + switch (method) { + case QRCodeMethod::Numeric: + bit_num = 10 * (input_size / 3); + if (input_size % 3 == 2) + bit_num += 7; + else if (input_size % 3 == 1) + bit_num += 4; + break; + case QRCodeMethod::Alphabetic: + bit_num = 11 * (input_size / 2); + if (input_size % 2 == 1) + bit_num += 6; + break; + case QRCodeMethod::Byte: + bit_num = input_size * 8; + break; + } + + return bit_num; +} + diff --git a/QRCodeLibrary/Encode.hpp b/QRCodeLibrary/Encode.hpp new file mode 100644 index 0000000..1578cb3 --- /dev/null +++ b/QRCodeLibrary/Encode.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include "Method.hpp" + +using namespace std; + +class Encode +{ +public: + static unsigned calculate_encoded_input_size(unsigned input_size, QRCodeMethod method); +}; + diff --git a/QRCodeLibrary/QRCodeLibrary.vcxproj b/QRCodeLibrary/QRCodeLibrary.vcxproj index 3ea7582..708b399 100644 --- a/QRCodeLibrary/QRCodeLibrary.vcxproj +++ b/QRCodeLibrary/QRCodeLibrary.vcxproj @@ -151,6 +151,7 @@ + @@ -158,6 +159,7 @@ + Create diff --git a/QRCodeLibrary/QRCodeLibrary.vcxproj.filters b/QRCodeLibrary/QRCodeLibrary.vcxproj.filters index cf9eef8..7113336 100644 --- a/QRCodeLibrary/QRCodeLibrary.vcxproj.filters +++ b/QRCodeLibrary/QRCodeLibrary.vcxproj.filters @@ -30,6 +30,9 @@ Header Files + + Header Files + @@ -44,5 +47,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/tests/Encode_test.cpp b/tests/Encode_test.cpp new file mode 100644 index 0000000..bc932b4 --- /dev/null +++ b/tests/Encode_test.cpp @@ -0,0 +1,21 @@ +#include "pch.h" + +#define protected public +#define private public + +#include "../QRCodeLibrary/Encode.hpp" + +TEST(EncodeTests, CalculatesEncodedInputSize) { + EXPECT_EQ(Encode::calculate_encoded_input_size(3, QRCodeMethod::Numeric), 10); + EXPECT_EQ(Encode::calculate_encoded_input_size(3 + 1, QRCodeMethod::Numeric), 10 + 4); + EXPECT_EQ(Encode::calculate_encoded_input_size(3*3 + 1, QRCodeMethod::Numeric), 10*3 + 4); + EXPECT_EQ(Encode::calculate_encoded_input_size(3 + 2, QRCodeMethod::Numeric), 10 + 7); + + EXPECT_EQ(Encode::calculate_encoded_input_size(2, QRCodeMethod::Alphabetic), 11); + EXPECT_EQ(Encode::calculate_encoded_input_size(2*3, QRCodeMethod::Alphabetic), 11*3); + EXPECT_EQ(Encode::calculate_encoded_input_size(2 + 1, QRCodeMethod::Alphabetic), 11 + 6); + EXPECT_EQ(Encode::calculate_encoded_input_size(2*3 + 1, QRCodeMethod::Alphabetic), 11*3 + 6); + + EXPECT_EQ(Encode::calculate_encoded_input_size(5, QRCodeMethod::Byte), 5 * 8); + EXPECT_EQ(Encode::calculate_encoded_input_size(10, QRCodeMethod::Byte), 10*8); +} \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index 6328d6d..ab443c2 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -36,6 +36,7 @@ +