Added encoded input size calculation
This commit is contained in:
parent
d4c6eff155
commit
1127ffa916
29
QRCodeLibrary/Encode.cpp
Normal file
29
QRCodeLibrary/Encode.cpp
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
14
QRCodeLibrary/Encode.hpp
Normal file
14
QRCodeLibrary/Encode.hpp
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "Method.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Encode
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static unsigned calculate_encoded_input_size(unsigned input_size, QRCodeMethod method);
|
||||||
|
};
|
||||||
|
|
@ -151,6 +151,7 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Encode.hpp" />
|
||||||
<ClInclude Include="framework.h" />
|
<ClInclude Include="framework.h" />
|
||||||
<ClInclude Include="Method.hpp" />
|
<ClInclude Include="Method.hpp" />
|
||||||
<ClInclude Include="pch.h" />
|
<ClInclude Include="pch.h" />
|
||||||
@ -158,6 +159,7 @@
|
|||||||
<ClInclude Include="Tables.hpp" />
|
<ClInclude Include="Tables.hpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Encode.cpp" />
|
||||||
<ClCompile Include="Method.cpp" />
|
<ClCompile Include="Method.cpp" />
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
|
||||||
|
@ -30,6 +30,9 @@
|
|||||||
<ClInclude Include="Tables.hpp">
|
<ClInclude Include="Tables.hpp">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Encode.hpp">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
@ -44,5 +47,8 @@
|
|||||||
<ClCompile Include="Tables.cpp">
|
<ClCompile Include="Tables.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Encode.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
21
tests/Encode_test.cpp
Normal file
21
tests/Encode_test.cpp
Normal file
@ -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);
|
||||||
|
}
|
@ -36,6 +36,7 @@
|
|||||||
<ClInclude Include="pch.h" />
|
<ClInclude Include="pch.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Encode_test.cpp" />
|
||||||
<ClCompile Include="Method_test.cpp" />
|
<ClCompile Include="Method_test.cpp" />
|
||||||
<ClCompile Include="QRCode_test.cpp" />
|
<ClCompile Include="QRCode_test.cpp" />
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user