-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added barcode type dection (EAN-8, EAN-13, UPC-A, Code39) and some un…
…it tests for that. Version 1.0.5.0
- Loading branch information
Showing
15 changed files
with
534 additions
and
58 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,110 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProductVersion> | ||
</ProductVersion> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>{7513FB40-6034-4002-B434-603E57CDCF44}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>BarcodeSimulator.Tests</RootNamespace> | ||
<AssemblyName>BarcodeSimulator.Tests</AssemblyName> | ||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
<DebugSymbols>true</DebugSymbols> | ||
<OutputPath>bin\x64\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<DebugType>full</DebugType> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<ErrorReport>prompt</ErrorReport> | ||
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets> | ||
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
<OutputPath>bin\x64\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<Optimize>true</Optimize> | ||
<DebugType>pdbonly</DebugType> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<ErrorReport>prompt</ErrorReport> | ||
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets> | ||
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> | ||
<DebugSymbols>true</DebugSymbols> | ||
<OutputPath>bin\x86\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<DebugType>full</DebugType> | ||
<PlatformTarget>x86</PlatformTarget> | ||
<ErrorReport>prompt</ErrorReport> | ||
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets> | ||
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> | ||
<OutputPath>bin\x86\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<Optimize>true</Optimize> | ||
<DebugType>pdbonly</DebugType> | ||
<PlatformTarget>x86</PlatformTarget> | ||
<ErrorReport>prompt</ErrorReport> | ||
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets> | ||
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="nunit.framework, Version=2.6.0.12051, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core"> | ||
<RequiredTargetFramework>3.5</RequiredTargetFramework> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> | ||
<Visible>False</Visible> | ||
</CodeAnalysisDependentAssemblyPaths> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="BarcodeTests.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\BarcodeSimulator\BarcodeSimulator.csproj"> | ||
<Project>{CF8A1048-009D-4892-9BDC-94864C2A4D57}</Project> | ||
<Name>BarcodeSimulator</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
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,75 @@ | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
|
||
namespace BarcodeSimulator.Tests | ||
{ | ||
|
||
[TestFixture] | ||
public class BarcodeTests | ||
{ | ||
|
||
[Test] | ||
public void IsUpcA_ValidUpcACodes_ReturnsTrue([Values("036000291452", "074312058004")] string code) | ||
{ | ||
Assert.IsTrue(Barcode.IsUpcA(code), "Checking code " + code); | ||
} | ||
|
||
[Test] | ||
public void IsUpcA_InvalidUpcACodes_ReturnsTrue([Values("036000291459", "074312058009")] string code) | ||
{ | ||
Assert.IsFalse(Barcode.IsUpcA(code), "Checking code " + code); | ||
} | ||
|
||
[Test(Description = "Make sure the codes are identified as invalid and no exceptions are thrown.")] | ||
public void IsUpcA_RightLengthButContainsLetters_ReturnsFalse([Values("X36000291459", "07431205800B")] string code) { | ||
Assert.IsFalse(Barcode.IsEan13(code)); | ||
} | ||
|
||
[Test] | ||
public void IsEan13_ValidEan13Codes_ReturnsTrue( | ||
[Values("4006381333931", "4005808515608" /* Nivea cream */, "5030917071027" /* Call of Duty MW2 Euro */)] string code) | ||
{ | ||
Assert.IsTrue(Barcode.IsEan13(code)); | ||
} | ||
|
||
[Test(Description = "Make sure the codes are identified as invalid and no exceptions are thrown.")] | ||
public void IsEan13_RightLengthButContainsLetters_ReturnsFalse([Values("400580851560X", "B030917071027")] string code) | ||
{ | ||
Assert.IsFalse(Barcode.IsEan13(code)); | ||
} | ||
|
||
[Test] | ||
public void IsEan13_ValidEan8Codes_ReturnsTrue([Values("73123453", "96385074", "73513537")] string code) | ||
{ | ||
Assert.IsTrue(Barcode.IsEan8(code)); | ||
} | ||
|
||
public void IsCode39_ValidCode39_ReturnsTrue([Values("*HELLO*", "*THIS IS CODE-39*")] string code) | ||
{ | ||
Assert.IsTrue(Barcode.IsCode39(code)); | ||
} | ||
|
||
[Test] | ||
public void IsCode39_InvalidCode39_ReturnsFalse([Values("*MISSING END", "MISSING BEGINNING*", "*LOWER case*", "*INVALID CHAR ÅÄÖ*", "*INVALID SYMBOL £*")] string code) | ||
{ | ||
Assert.IsFalse(Barcode.IsCode39(code)); | ||
} | ||
|
||
[Test] | ||
public void GetTypeName() | ||
{ | ||
var expectations = new Dictionary<string, string> | ||
{ | ||
{"036000291452", "UPC-A"}, | ||
{"074312058004", "UPC-A"}, | ||
{"5030917071027", "EAN-13"}, | ||
{"4005808515608", "EAN-13"}, | ||
{"73123453", "EAN-8"}, | ||
{"73513537", "EAN-8"} | ||
}; | ||
|
||
foreach (var expectation in expectations) | ||
Assert.AreEqual(expectation.Value, Barcode.GetTypeName(expectation.Key), "Code {0} should be {1}", expectation.Key, expectation.Value); | ||
} | ||
} | ||
} |
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,35 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyTitle("BarcodeSimulator.Tests")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("BarcodeSimulator.Tests")] | ||
[assembly: AssemblyCopyright("Copyright © 2012")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("edf47845-e519-4d44-ac38-27ed70736e6c")] | ||
|
||
// Version information for an assembly consists of the following four values: | ||
// | ||
// Major Version | ||
// Minor Version | ||
// Build Number | ||
// Revision | ||
// | ||
// You can specify all the values or you can default the Build and Revision Numbers | ||
// by using the '*' as shown below: | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="NUnit" version="2.6.0.12054" /> | ||
</packages> |
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,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace BarcodeSimulator | ||
{ | ||
|
||
public static class Barcode | ||
{ | ||
|
||
public static string GetTypeName(string input) | ||
{ | ||
if (IsUpcA(input)) return "UPC-A"; | ||
if (IsEan13(input)) return "EAN-13"; | ||
if (IsEan8(input)) return "EAN-8"; | ||
if (IsCode39(input)) return "Code39"; | ||
return "Text"; | ||
} | ||
|
||
public static bool IsUpcA(string input) | ||
{ | ||
if (input.Length != 12) | ||
return false; | ||
|
||
if (!input.ToCharArray().All(char.IsDigit)) | ||
return false; | ||
|
||
var digits = input.ToCharArray(0, 11).Select(CharCodeOfNumberToInt).ToArray(); | ||
var odds = Odds(digits).Sum(); | ||
var evens = Evens(digits).Sum(); | ||
var result = 10 - ((odds*3 + evens) % 10); | ||
|
||
return CharCodeOfNumberToInt(input[11]) == result; | ||
} | ||
|
||
public static bool IsEan13(string input) | ||
{ | ||
return input.Length == 13 && IsEan(input); | ||
} | ||
|
||
public static bool IsEan8(string input) | ||
{ | ||
return input.Length == 8 && IsEan(input); | ||
} | ||
|
||
public static bool IsCode39(string input) | ||
{ | ||
return input.Length > 2 && input[0] == '*' && input.Last() == '*' && input.ToCharArray().All(IsCode39Char); | ||
} | ||
|
||
private static bool IsCode39Char(char c) | ||
{ | ||
return (c >= 0x41 && c <= 0x5a) // A-Z (uppercase only) | ||
|| (c >= 0x30 && c <= 0x39) // 0-9 | ||
|| (new[] {'*', ' ', '-', '$', '%', '.', '/', '+'}.Contains(c)); | ||
} | ||
|
||
private static bool IsEan(string input) | ||
{ | ||
if (!input.ToCharArray().All(char.IsDigit)) | ||
return false; | ||
|
||
// take each character, turn it into an integer, then reverse the list | ||
// we reverse it because every odd-numbered (1-based counting) digit counting from the END | ||
// is multiplied by 3. | ||
var digits = input.ToCharArray(0, input.Length - 1).Select(CharCodeOfNumberToInt).Reverse().ToArray(); | ||
var odds = Odds(digits).Sum() * 3; | ||
var evens = Evens(digits).Sum(); | ||
var total = odds + evens; | ||
|
||
return 10 - (total % 10) == CharCodeOfNumberToInt(input.Last()); | ||
} | ||
|
||
/// <summary> | ||
/// Takes the ASCII character code of a number and converts it to an integer. | ||
/// </summary> | ||
/// <param name="c"></param> | ||
/// <returns></returns> | ||
/// <example>>CharCodeOfNumberToInt('8') => 8</example> | ||
private static int CharCodeOfNumberToInt(char c) | ||
{ | ||
var code = (int) c; | ||
|
||
if (code < 48 || code > 57) | ||
throw new ArgumentException("This character code is not for a number", "c"); | ||
|
||
return code - 48; | ||
} | ||
|
||
public static IEnumerable<T> Odds<T>(T[] source) | ||
{ | ||
return Enumerable.Range(0, source.Count()).Where(i => i%2 == 0).Select(i => source[i]); | ||
} | ||
|
||
public static IEnumerable<T> Evens<T>(T[] source) { | ||
return Enumerable.Range(0, source.Count()).Where(i => i % 2 != 0).Select(i => source[i]); | ||
} | ||
} | ||
} |
Oops, something went wrong.