Advertisement
damoncard

Tester

Nov 21st, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.54 KB | None | 0 0
  1. package main
  2.  
  3. /*  fmt = I/O (Abbre from Format)
  4.     ./tools = Another package which contains all of file(class) in that package (folder) */
  5. import ("fmt"
  6.     "./tools");
  7.  
  8. // Test Insertion Sort in sorting file (Decrease and Conquer)
  9. func test_insertSort(arr []int) {
  10.     fmt.Println("Before sort: ", arr)
  11.     fmt.Println("After sort: ", tools.InsertionSort(arr))
  12. }
  13.  
  14. // Test Counting Sort in sorting file (Space and Time)
  15. func test_countingSort(arr []int) {
  16.     fmt.Println("Before sort: ", arr)
  17.     fmt.Println("After sort: ", tools.CountingSort(arr))
  18. }
  19.  
  20. // Test Distribution Counting in sorting file (Space and Time)
  21. func test_distributeCounting(arr []int) {
  22.     fmt.Println("Before sort: ", arr)
  23.     fmt.Println("After sort: ", tools.DistributionCounting(arr, tools.Min(arr), tools.Max(arr)))
  24. }
  25.  
  26. // Test find Median in aggregate file (Decrease and Conquer)
  27. func test_median(arr []int) {
  28.     fmt.Println("Median is: ", tools.Median(arr, 0, len(arr)-1))
  29. }
  30.  
  31. // Test find Mode in aggregate file (Transform and Conquer)
  32. // Sort -> Find (This case use Insertion sort)
  33. func test_mode(arr []int) {
  34.     fmt.Println("Mode is: ", tools.Mode(tools.InsertionSort(arr)))
  35. }
  36.  
  37. // Test Russian Multiplication of Decimal input in russianMul (Decrease and Conquer)
  38. func test_decimalMul() {
  39.     var first, second int
  40.     fmt.Print("Insert Integers: ")
  41.     fmt.Scan(&first, &second)
  42.     fmt.Println("Your answer is: ", tools.DecimalMul(tools.MinandMax(first, second)))
  43. }
  44.  
  45. // Test Russian Multiplication of Binary input in russianMul (NULL)
  46. func test_binaryMul() {
  47.     var first, second string
  48.     fmt.Print("Insert Binaries: ")
  49.     // Input in Binary format e.g. 110
  50.     fmt.Scan(&first, &second)
  51.     fmt.Println("Your answer is: ", tools.BinaryMul(first, second))
  52. }
  53.  
  54. // Test find Transitive Closure in directedGraph (Dynamic Programming)
  55. func test_transitiveClosure(matrix [][]int) {
  56.     fmt.Println("Matrix before perform: ", matrix)
  57.     fmt.Println("Matrix after perform: ", tools.TransitiveClosure(matrix))
  58. }
  59.  
  60. // Test find Distance Matrix in directedGraph (Dynamic Programming)
  61. func test_distanceMatrix(matrix [][]int) {
  62.     fmt.Println("Matrix before perform: ", matrix)
  63.     fmt.Println("Matrix after perform: ", tools.DistanceMatrix(matrix))
  64. }
  65.  
  66. // Test calculation Polynomial in polynomial (Transfrom and Conquer)
  67. func test_HornerPolynomial() {
  68.     var n, x int
  69.     fmt.Print("Insert number of coef: ")
  70.     fmt.Scan(&n)
  71.     var arr = make([]int, n)
  72.     fmt.Print("Insert coefs: ")
  73.     // Input coefficients in sequence (2x+3) -> 2 3
  74.     i := 0
  75.     for i < n {
  76.         fmt.Scan(&arr[i])
  77.         i++
  78.     }
  79.     fmt.Print("Insert input: ")
  80.     // Input x which replace x in the polynomial function
  81.     fmt.Scan(&x)
  82.     fmt.Println("Your result is: ", tools.HornerPo(arr, x))
  83. }
  84.  
  85. // Test find Pattern in Text input in matching (Space and Time)
  86. func test_Horspool() {
  87.     var p, t string
  88.     fmt.Print("Insert text: ")
  89.     // Scan text of the file "NOT" include white-space
  90.     fmt.Scan(&t)
  91.     fmt.Print("Insert pattern: ")
  92.     // Scan pattern which use to seach in the text input
  93.     fmt.Scan(&p)
  94.     // Method result the first index of matched text (not found = -1)
  95.     var result int = tools.HorspoolMatching(tools.ShiftTable(p), p, t)
  96.     if result == -1 {
  97.         fmt.Println("Your pattern's not in the text")
  98.     } else {
  99.         fmt.Println("Your patter starts with ", result)
  100.     }
  101. }
  102.  
  103. // Necessary function
  104. func inputArray() []int {
  105.     var n int
  106.     fmt.Print("Insert length of array: ")
  107.     fmt.Scan(&n)
  108.     var arr = make([]int, n)
  109.     fmt.Print("Insert array: ")
  110.     i := 0
  111.     for i < n {
  112.         fmt.Scan(&arr[i])
  113.         i++
  114.     }
  115.     return arr
  116. }
  117.  
  118. // Necessary function
  119. func inputMatrix() [][]int {
  120.     var n int
  121.     fmt.Print("How many vertices: ")
  122.     fmt.Scan(&n)
  123.     var mat = make ([][]int, n)
  124.     for index := range mat {
  125.         mat[index] = make([]int, n)
  126.     }
  127.     i, j := 0, 0
  128.     for i < n {
  129.         fmt.Print("Insert elements of ", i+1, " row: ")
  130.         for j < n {
  131.             fmt.Scan(&mat[i][j])
  132.             j++
  133.         }
  134.         i++
  135.         j = 0
  136.     }
  137.     return mat
  138. }
  139.  
  140. func main() {
  141.     var s string
  142.     fmt.Print("What do you want to test: ")
  143.     fmt.Scan(&s)
  144.     // Switch automatically provided break in each case
  145.     switch s {
  146.         case "insertionsort": test_insertSort(inputArray())
  147.         case "countingsort": test_countingSort(inputArray())
  148.         case "distributioncounting": test_distributeCounting(inputArray())
  149.         case "median": test_median(inputArray())
  150.         case "mode": test_mode(inputArray())
  151.         case "decimalmul": test_decimalMul()
  152.         case "binarymul": test_binaryMul()
  153.         case "hornerpo" : test_HornerPolynomial()
  154.         case "horspool": test_Horspool()
  155.         case "transitiveclosure": test_transitiveClosure(inputMatrix())
  156.         case "distancematrix": test_distanceMatrix(inputMatrix())
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement