Bladeren bron

add new source and tests, refactor test dir structure

Gary Paduana 8 jaren geleden
bovenliggende
commit
c025a432a6

+ 2 - 0
.gitignore

@@ -5,3 +5,5 @@ build/
 
 **/.idea/workspace.xml 
 **/.idea/tasks.xml
+
+.DS_Store

+ 1 - 2
.idea/gradle.xml

@@ -5,8 +5,7 @@
       <GradleProjectSettings>
         <option name="distributionType" value="LOCAL" />
         <option name="externalProjectPath" value="$PROJECT_DIR$" />
-        <option name="gradleHome" value="$USER_HOME$/Downloads/gradle-3.3" />
-        <option name="gradleJvm" value="#JAVA_HOME" />
+        <option name="gradleHome" value="$USER_HOME$/Downloads/gradle-3.4.1" />
         <option name="modules">
           <set>
             <option value="$PROJECT_DIR$" />

+ 0 - 11
.idea/libraries/Gradle__org_codehaus_groovy_groovy_all_2_4_8.xml

@@ -1,11 +0,0 @@
-<component name="libraryTable">
-  <library name="Gradle: org.codehaus.groovy:groovy-all:2.4.8">
-    <CLASSES>
-      <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.4.8/b2ddd0b4b2dd7598ccb769cc2eb37ff8d683e7c0/groovy-all-2.4.8.jar!/" />
-    </CLASSES>
-    <JAVADOC />
-    <SOURCES>
-      <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.4.8/c0b188118d0bbee615ee601c048b14c7772d7e41/groovy-all-2.4.8-sources.jar!/" />
-    </SOURCES>
-  </library>
-</component>

+ 11 - 0
.idea/libraries/Gradle__org_codehaus_groovy_groovy_all_2_4_9.xml

@@ -0,0 +1,11 @@
+<component name="libraryTable">
+  <library name="Gradle: org.codehaus.groovy:groovy-all:2.4.9">
+    <CLASSES>
+      <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.4.9/3334e99a8baae12d6e014d444149e337ceb99a00/groovy-all-2.4.9.jar!/" />
+    </CLASSES>
+    <JAVADOC />
+    <SOURCES>
+      <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.4.9/35c41a130264a801b171d8ad74b27f62b34bb6fa/groovy-all-2.4.9-sources.jar!/" />
+    </SOURCES>
+  </library>
+</component>

+ 0 - 3
.idea/modules.xml

@@ -5,9 +5,6 @@
       <module fileurl="file://$PROJECT_DIR$/.idea/modules/groovy-tools.iml" filepath="$PROJECT_DIR$/.idea/modules/groovy-tools.iml" />
       <module fileurl="file://$PROJECT_DIR$/.idea/modules/groovy-tools_main.iml" filepath="$PROJECT_DIR$/.idea/modules/groovy-tools_main.iml" group="groovy-tools" />
       <module fileurl="file://$PROJECT_DIR$/.idea/modules/groovy-tools_test.iml" filepath="$PROJECT_DIR$/.idea/modules/groovy-tools_test.iml" group="groovy-tools" />
-      <module fileurl="file://$PROJECT_DIR$/src/main/main.iml" filepath="$PROJECT_DIR$/src/main/main.iml" />
-      <module fileurl="file://$PROJECT_DIR$/src/main/resources.iml" filepath="$PROJECT_DIR$/src/main/resources.iml" />
-      <module fileurl="file://$PROJECT_DIR$/src/main/test.iml" filepath="$PROJECT_DIR$/src/main/test.iml" />
     </modules>
   </component>
 </project>

+ 4 - 4
.idea/modules/groovy-tools_main.iml

@@ -1,17 +1,17 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <module external.linked.project.id="groovy-tools:main" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="" external.system.module.type="sourceSet" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
     <output url="file://$MODULE_DIR$/../../build/classes/main" />
     <exclude-output />
     <content url="file://$MODULE_DIR$/../../src/main">
       <sourceFolder url="file://$MODULE_DIR$/../../src/main/groovy" isTestSource="false" />
       <sourceFolder url="file://$MODULE_DIR$/../../src/main/java" isTestSource="false" />
-      <sourceFolder url="file://$MODULE_DIR$/../../src/main/test" isTestSource="true" />
-      <sourceFolder url="file://$MODULE_DIR$/../../src/main/resources" type="java-resource" />
+      <sourceFolder url="file://$MODULE_DIR$/../../src/test" isTestSource="true" />
+      <sourceFolder url="file://$MODULE_DIR$/../../src/main/resources" type="java-test-resource" />
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
-    <orderEntry type="library" name="Gradle: org.codehaus.groovy:groovy-all:2.4.8" level="project" />
+    <orderEntry type="library" name="Gradle: org.codehaus.groovy:groovy-all:2.4.9" level="project" />
     <orderEntry type="library" name="Gradle: com.sun.mail:javax.mail:1.5.3" level="project" />
     <orderEntry type="library" name="Gradle: org.spockframework:spock-core:1.0-groovy-2.4" level="project" />
     <orderEntry type="library" name="Gradle: junit:junit:4.12" level="project" />

+ 2 - 2
.idea/modules/groovy-tools_test.iml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <module external.linked.project.id="groovy-tools:test" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="" external.system.module.type="sourceSet" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
     <output-test url="file://$MODULE_DIR$/../../build/classes/test" />
     <exclude-output />
     <content url="file://$MODULE_DIR$/../../src/test">
@@ -11,7 +11,7 @@
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="module" module-name="groovy-tools_main" />
-    <orderEntry type="library" name="Gradle: org.codehaus.groovy:groovy-all:2.4.8" level="project" />
+    <orderEntry type="library" name="Gradle: org.codehaus.groovy:groovy-all:2.4.9" level="project" />
     <orderEntry type="library" name="Gradle: com.sun.mail:javax.mail:1.5.3" level="project" />
     <orderEntry type="library" name="Gradle: org.spockframework:spock-core:1.0-groovy-2.4" level="project" />
     <orderEntry type="library" name="Gradle: junit:junit:4.12" level="project" />

+ 1 - 1
build.gradle

@@ -5,7 +5,7 @@ repositories {
 }
 
 dependencies {
-    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.8'
+    compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.9'
     compile group: 'com.sun.mail', name: 'javax.mail', version: '1.5.3'
     compile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'
     compile group: 'junit', name: 'junit', version: '4.12'

+ 35 - 0
src/main/groovy/com/garypaduana/groovytools/data/Calculations.groovy

@@ -0,0 +1,35 @@
+package com.garypaduana.groovytools.data
+
+import com.garypaduana.groovytools.formatting.StringCleaner
+
+class Calculations {
+
+    static def calculateParity(String hex, int parityBitLength){
+
+        hex = StringCleaner.stripNonHexCharacters(hex)
+
+        if(parityBitLength > 64){
+            throw new IllegalArgumentException("parityBitLength must be less than or equal to 64")
+        }
+
+        if((hex.length() * 4) % parityBitLength != 0) {
+            throw new IllegalArgumentException("Invalid word length found!")
+        }
+
+        def pieces = []
+        StringBuilder sb = new StringBuilder(hex)
+
+        while(sb.length() >= (parityBitLength / 4)){
+            def end = (int) (parityBitLength / 4)
+            pieces.add(Long.decode("0x" + sb.substring(0, end)))
+            sb.delete(0, end)
+        }
+
+        long parity = 0
+        for(long piece : pieces){
+            parity = parity ^ piece
+        }
+
+        return Long.toHexString(parity).toLowerCase()
+    }
+}

+ 115 - 0
src/main/groovy/com/garypaduana/groovytools/formatting/StringCleaner.groovy

@@ -0,0 +1,115 @@
+package com.garypaduana.groovytools.formatting
+
+import java.text.DecimalFormat
+
+class StringCleaner {
+
+    static def stripNonHexCharacters(String text){
+        return text.replaceAll("[^0-9ABCDEFabcdef]", '')
+    }
+
+    /**
+     *
+     * @param charsPerWord
+     * @param wordsPerLine
+     * @param text
+     * @return map with keys ['parityText', 'label', 'parityRowCounter', 'ascii']
+     */
+    static def formatParityText(int charsPerWord, int wordsPerLine, String text){
+        StringBuilder sb = new StringBuilder()
+        StringBuilder counter = new StringBuilder()
+        counter.append("00000000 ")
+        StringBuilder ascii = new StringBuilder()
+
+        text = stripNonHexCharacters(text)
+
+        int bits = text.length() * 4
+        int bytes = bits % 8 == 0 ? (bits / 8) : (bits / 8 + 1)
+        String label = "Length: ${customFormat('###,###,###', text.length())} hex chars; " +
+                "${customFormat('###,###,###', bits)} bits; " +
+                "${customFormat('###,###,###', bytes)} bytes"
+
+        // We want the output to look like this:
+        // 1234 5233 5923 4234 ab32 def9 acd3 92ff [...]
+        for(int i = 0; i < text.length(); i += charsPerWord){
+            if(i % (charsPerWord * wordsPerLine) == 0 && i > 0){
+                // remove trailing space
+                sb.setLength(sb.length() - 1)
+                sb.append("\n")
+                ascii.append("\n")
+                counter.append("\n" + Integer.toString((i / 2).intValue(), 8).padLeft(8, '0') + " ")
+            }
+
+            List<String> asciiBytes = new ArrayList<String>()
+
+            for(int j = 0; j < charsPerWord; j += 2){
+                asciiBytes.add(safeSubstring(text, i + j, i + j + 2))
+            }
+
+            for(String asciiByte : asciiBytes){
+                if(asciiByte.length() > 0){
+                    String toAppend = String.valueOf((char) (Integer.decode("0x" + asciiByte) & 0xFF))
+                    toAppend = toAppend.replaceAll(/[^\p{Print}]/, String.valueOf((char) Integer.decode("0x00")))
+                    ascii.append(toAppend)
+                }
+            }
+
+            int end = (i + charsPerWord > text.length()) ? (text.length()) : (i + charsPerWord)
+            sb.append(text.substring(i, end))
+            sb.append(" ")
+        }
+        sb.setLength(sb.toString().trim().length())
+        return ['parityText': sb.toString(), 'label': label, 'parityRowCounter': counter.toString(), 'ascii': ascii.toString()]
+    }
+
+    /**
+     * Convenience method for obtaining a substring without having the client perform any size checks.
+     * @param str - the source string
+     * @param start - the start position
+     * @param end - the end position
+     * @return the desired substring or less if the request couldn't be fully satisfied
+     */
+    static def safeSubstring(String str, int start, int end){
+        if(start < 0){
+            start = 0
+        }
+
+        if(end > str.length()){
+            end = str.length()
+        }
+
+        if(end < start || start >= str.length()){
+            return ""
+        }
+
+        return str.substring(start, end)
+    }
+
+    static def customFormat(String pattern, int value){
+        DecimalFormat decimalFormat = new DecimalFormat(pattern);
+        return decimalFormat.format(value);
+    }
+
+    static def customFormat(String pattern, double value){
+        DecimalFormat decimalFormat = new DecimalFormat(pattern);
+        return decimalFormat.format(value);
+    }
+
+    static byte[] decodeMessyHexToByteArray(String text){
+        text = stripNonHexCharacters(text)
+
+        int bitCount = text.length() * 4
+        int byteCount = bitCount % 8 == 0 ? (bitCount / 8) : (bitCount / 8 + 1)
+
+        byte[] bytes = new byte[byteCount]
+
+        int j = 0
+        for(int i = 0; i < text.length(); i += 2){
+            int end = (i + 2) <= text.length() ? (i + 2) : text.length()
+            bytes[j] = 0xFF & Integer.valueOf(text.substring(i, end), 16)
+            j++
+        }
+
+        return bytes
+    }
+}

+ 1 - 0
src/main/test.iml

@@ -8,5 +8,6 @@
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
     <orderEntry type="library" name="groovy-2.4.8" level="application" />
+    <orderEntry type="library" name="groovy-2.4.9" level="application" />
   </component>
 </module>

+ 0 - 0
src/main/test/com/garypaduana/groovytools/algorithm/TestFermatsLittleTheorem.groovy → src/test/groovy/com/garypaduana/groovytools/algorithm/TestFermatsLittleTheorem.groovy


+ 0 - 0
src/main/test/com/garypaduana/groovytools/algorithm/TestSort.groovy → src/test/groovy/com/garypaduana/groovytools/algorithm/TestSort.groovy


+ 15 - 0
src/test/groovy/com/garypaduana/groovytools/data/TestCalculations.groovy

@@ -0,0 +1,15 @@
+package com.garypaduana.groovytools.data
+
+import spock.lang.Specification
+
+class TestCalculations extends Specification {
+
+    def "test calculateParity"(){
+        setup:
+            def source = "AAnnn"
+        when:
+            def parity = Calculations.calculateParity(source, 8)
+        then:
+            parity.equals("aa")
+    }
+}

+ 0 - 0
src/main/test/com/garypaduana/groovytools/data/structure/TestDataStructures.groovy → src/test/groovy/com/garypaduana/groovytools/data/structure/TestDataStructures.groovy


+ 55 - 0
src/test/groovy/com/garypaduana/groovytools/formatting/TestStringCleaner.groovy

@@ -0,0 +1,55 @@
+package com.garypaduana.groovytools.formatting
+
+import spock.lang.Specification
+
+class TestStringCleaner extends Specification{
+
+    def "test cleaning non hex from string"(){
+        setup:
+            def testCase = "77%\$ueallp009eacd"
+        when:
+            def result = StringCleaner.stripNonHexCharacters(testCase)
+        then:
+            result.equals("77ea009eacd")
+    }
+
+    def "test safe substring"(){
+        setup:
+            def source = "This is the test string"
+        when:
+            def result1 = StringCleaner.safeSubstring(source, 0, 100)
+            def result2 = StringCleaner.safeSubstring(source, -10, 5)
+            def result3 = StringCleaner.safeSubstring(source, -10, 200)
+            def result4 = StringCleaner.safeSubstring(source, 2, 5)
+            def result5 = StringCleaner.safeSubstring(source, 50, 0)
+        then:
+            result1.equals(source)
+            result2.equals("This ")
+            result3.equals(source)
+            result4.equals("is ")
+            result5.equals("")
+    }
+
+    def "test formatParityText"(){
+        setup:
+            def source = "012345678rtyuiop"
+        when:
+            def result = StringCleaner.formatParityText(4, 8, source)
+        then:
+            result.parityText.equals("0123 4567 8")
+            result.label.equals("Length: 9 hex chars; 36 bits; 5 bytes")
+            result.parityRowCounter.equals("00000000 ")
+            println "ascii value: ${result.ascii}"
+    }
+
+    def "test decodeMessyHexToByteArray"(){
+        setup:
+            def source = "44uu66ppp999"
+        when:
+            def result = StringCleaner.decodeMessyHexToByteArray(source)
+        then:
+            result.length == 4
+            result == [68, 102, -103, 9]
+
+    }
+}

+ 0 - 0
src/main/test/com/garypaduana/groovytools/system/TestFiles.groovy → src/test/groovy/com/garypaduana/groovytools/system/TestFiles.groovy


+ 0 - 0
src/main/test/com/garypaduana/groovytools/system/TestProcess.groovy → src/test/groovy/com/garypaduana/groovytools/system/TestProcess.groovy