Update unit tests and add performance analyze
This commit is contained in:
		@@ -29,3 +29,11 @@ tasks.test {
 | 
			
		||||
    useJUnitPlatform()
 | 
			
		||||
    jvmArgs = listOf("-Dfast=true")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
tasks.register("runPerformanceAnalyze", JavaExec::class.java)
 | 
			
		||||
tasks.named<JavaExec>("runPerformanceAnalyze") {
 | 
			
		||||
    dependsOn("testClasses")
 | 
			
		||||
    group = "verification"
 | 
			
		||||
    classpath = sourceSets.test.get().runtimeClasspath
 | 
			
		||||
    mainClass.set("com.cleverthis.interview.PerformanceAnalyze")
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,47 @@
 | 
			
		||||
package com.cleverthis.interview;
 | 
			
		||||
 | 
			
		||||
import com.cleverthis.interview.padlock.PadlockImpl;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Performance test but not mean to run in unit test.
 | 
			
		||||
 */
 | 
			
		||||
public class PerformanceAnalyze {
 | 
			
		||||
    private static void solve(PadlockImpl padlock) {
 | 
			
		||||
        // TODO
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static final String RUN_NAME = "Boilerplate";
 | 
			
		||||
    private static final int TOTAL_RUN = 50;
 | 
			
		||||
    private static final int NUMPAD_SIZE = 7;
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        System.out.println("Run name: " + RUN_NAME);
 | 
			
		||||
        System.out.println("Total run: " + TOTAL_RUN);
 | 
			
		||||
        System.out.println("Numpad size: " + NUMPAD_SIZE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        long timeSum = 0;
 | 
			
		||||
        long writeSum = 0;
 | 
			
		||||
        for (int i = 0; i < TOTAL_RUN; i++) {
 | 
			
		||||
            PadlockImpl padlock = new PadlockImpl(NUMPAD_SIZE);
 | 
			
		||||
            padlock.resetCounter();
 | 
			
		||||
            long start = System.currentTimeMillis();
 | 
			
		||||
            solve(padlock);
 | 
			
		||||
            long end = System.currentTimeMillis();
 | 
			
		||||
            if (!padlock.isPasscodeCorrect()) throw new IllegalStateException(
 | 
			
		||||
                    "Invalid solution: passcode not correct after return");
 | 
			
		||||
            long dT = end - start;
 | 
			
		||||
            timeSum += dT;
 | 
			
		||||
            writeSum += padlock.getWriteCounter();
 | 
			
		||||
            System.out.println("Run #" + (i + 1) + ": time: " + dT + "ms; write: " + padlock.getWriteCounter());
 | 
			
		||||
        }
 | 
			
		||||
        System.out.println("Run time sum: " + timeSum + "ms");
 | 
			
		||||
        System.out.println("Write sum: " + writeSum);
 | 
			
		||||
        double avgTime = timeSum / (double) TOTAL_RUN;
 | 
			
		||||
        double avgWrite = writeSum / (double) TOTAL_RUN;
 | 
			
		||||
        System.out.println("Avg run time: " + avgTime + "ms");
 | 
			
		||||
        System.out.println("Avg write: " + avgWrite);
 | 
			
		||||
        System.out.println("Calculated estimate avg run time: " + (avgTime / 1000 + avgTime) + "s");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,30 +1,13 @@
 | 
			
		||||
package com.cleverthis.interview;
 | 
			
		||||
 | 
			
		||||
import com.cleverthis.interview.padlock.PadlockImpl;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is a simple placeholder to show how unit test works.
 | 
			
		||||
 * You can replace it with your own test.
 | 
			
		||||
 */
 | 
			
		||||
class SolutionTest {
 | 
			
		||||
    private void solve(PadlockImpl padlock) {
 | 
			
		||||
class SolutionTest extends SolutionTestBase {
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void solve(PadlockImpl padlock) {
 | 
			
		||||
        new Solution().solve(padlock);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void verify(){
 | 
			
		||||
        Random random = new Random();
 | 
			
		||||
        PadlockImpl padlock = new PadlockImpl(random.nextInt(1, 8));
 | 
			
		||||
 | 
			
		||||
        long startTime = System.currentTimeMillis();
 | 
			
		||||
        solve(padlock);
 | 
			
		||||
        long endTime = System.currentTimeMillis();
 | 
			
		||||
        assertTrue(padlock.isPasscodeCorrect());
 | 
			
		||||
        System.out.println("Time usage: " + (endTime - startTime) + "ms");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								src/test/java/com/cleverthis/interview/SolutionTestBase.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/test/java/com/cleverthis/interview/SolutionTestBase.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
package com.cleverthis.interview;
 | 
			
		||||
 | 
			
		||||
import com.cleverthis.interview.padlock.PadlockImpl;
 | 
			
		||||
import org.junit.jupiter.api.Test;
 | 
			
		||||
 | 
			
		||||
import static org.junit.jupiter.api.Assertions.assertTrue;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is a base class for verifying the correctness of the solution.
 | 
			
		||||
 */
 | 
			
		||||
public abstract class SolutionTestBase {
 | 
			
		||||
    
 | 
			
		||||
    /**
 | 
			
		||||
     * Implement your solution in this function.
 | 
			
		||||
     * */
 | 
			
		||||
    protected abstract void solve(PadlockImpl padlock);
 | 
			
		||||
 | 
			
		||||
    protected void verify(int numpadSize) {
 | 
			
		||||
        PadlockImpl padlock = new PadlockImpl(numpadSize);
 | 
			
		||||
        solve(padlock);
 | 
			
		||||
        assertTrue(padlock.isPasscodeCorrect());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    void verify1to7() {
 | 
			
		||||
        for (int i = 1; i <= 7; i++) {
 | 
			
		||||
            verify(i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user