Go language-unit test

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Today, nothing to see the next go language, the first is to see the cool Shell Mouse Daniel's article, and then download the installation, configuration environment, and so on, because I downloaded the installation version, so the environment variables do not have their own configuration. And then just went to the library to borrow two books: "Go language Programming" Xu Xiwei/Lu Guihua, "Go Language Cloud Power" Shonghong authoring, and then began to see.

Well, nonsense not much to say, directly into the subject:

In "Go language programming" I saw that he had some examples, in the second chapter of the book, the two sort of Go language implementations (quick sort and bubble sort) The source code of this article is from Go language programming Xu Xiwei/Lu Guihua

Sorting itself is not important, because before with C,java also realized, it is important to practice writing go language, familiar with the next grammar.

Environment:

I set the Gopath to: G:\go

Then all the files and directories are as follows:

Main program (SORTER.GO): G:\GO\SRC

Sorting algorithm (Bubblesort.go, qsort.go): G:\go\src\mysort (Note that this cannot be named as sort otherwise it will go wrong when you perform the run to install)

Test Class (Bubblesort_test.go, Qsort_test.go): G:\src\sort_test (note that the test class must end with _test.go, before the _ Test.go The main function is written in the end of the file, and then the compiler error is determined)

Source:

Soter.go

Package Mainimport ("Flag" "FMT" "Bufio" "IO" "OS" "StrConv" "Time" "Mysort") var infile *string = flag. String ("I", "infile", "File contains values for sorting") var outfile *string = flag. String ("O", "outfile", "File to receive sorted values") var algorithm *string = flag. String ("A", "Qsort", "Sort algorithm") Func readvalues (infile string) (values []int,err error) {file,err: = os. Open (infile) If err! = nil{fmt. Println ("Failed to open the input file", infile) return}defer file. Close () br: = Bufio. Newreader (file) values = make ([]int,0) For{line,isprefix,err1: = Br. ReadLine () if err1! = nil{if err1! = Io. Eof{err = err1}break}if Isprefix {fmt. Println ("A too long line seems unexpected.") RETURN}STR: = string (line) Value,err1: = StrConv. Atoi (str) if err1! = Nil{err = Err1return}values = Append (values,value)}return}func writevalues (values []int,outfile String) Error{file,err: = OS. Create (outfile) If err! = nil{fmt. Println ("Failed to create the output file", outfile) return Err}defer file. Close () for _,value: = range values{str: = StrConv. Itoa (value) file. WriteString (str + "\ n")}return Nil}func Main () {flag. Parse () if infile! = nil{fmt. Println ("infile =", *infile, "outfile =", *outfile, "algorithm =", *algorithm)}values,err: = Readvalues (*infile) If Err = = N IL{T1: = time. Now () switch *algorithm{case "qsort": Mysort. QuickSort (values) case "Bubblesort": Mysort. Bubblesort (values) default:fmt. Println ("Sorting algorithm", *algorithm, "is either unknown or unsupported.")} T2: = time. Now () Fmt. Println ("The sorting process costs", T2. Sub (T1), "to complete") writevalues (Values,*outfile)}else{fmt. PRINTLN (ERR)}}

Sorting algorithm:

Bubblesort.go

Package Mysortfunc Bubblesort (values []int) {flag: = Truefor I:=0;i < Len (values) -1;i++{flag = truefor J:=0;j<len (VA lues)-i-1;j++{if Values[j] > values[j+1]{values[j],values[j + 1] = values[j + 1],values[j]flag = false}//End If}//end For j=...if flag = = true{break}}//end for i= ...}

Qsort.go

Package Mysortfunc QuickSort (values []int,left,right int) {temp:=values[left]p: = lefti,j:=left,rightfor i <= j{for j& Gt;=p && Values[j] >= temp{j--}if (J >= p) {Values[p] = Values[j]p = j}if values[i] <= temp && i &L t;= p{i++}if i<=p {values[p] = Values[i]p = i}}values[p] = tempif p-left > 1{quicksort (values,left,p-1)}if Right-p & Gt 1{quicksort (values,p+1,right)}}func quickSort (values []int) {QuickSort (Values,0,len (values)-1)}


Test class:

Bubblesort_test.go

Package Sort_testimport ("Mysort" "testing") Func TestBubbleSort1 (t *testing. T) {values: = []int{5,4,3,2,1}mysort. Bubblesort (values) if values[0]! = 1 | | VALUES[1]! = 2 | | VALUES[2]! = 3| | VALUES[3]! = 4 | | VALUES[4]! = 5{t.error ("Bubblesort () failed, Got", Values, "excepted 1 2 3 4 5")}}func TestBubbleSort2 (t *testing. T) {values: = []int{5,5,3,2,1}mysort. Bubblesort (values) if values[0]! = 1 | | VALUES[1]! = 2 | | VALUES[2]! = 3| | VALUES[3]! = 5 | | VALUES[4]! = 5{t.error ("Bubblesort () failed, Got", Values, "excepted 1 2 3 5 5")}}func TestBubbleSort3 (t *testing. T) {values: = []int{5}mysort. Bubblesort (values) if values[0]! = 5{t.error ("Bubblesort () failed, Got", Values, "excepted 5")}}


Qsort_test.go
Package Sort_testimport ("Mysort" "testing") Func TestQuickSort1 (t *testing. T) {Values:=[]int{5,4,3,2,1}mysort. QuickSort (values) if values[0]! = 1 | | VALUES[1]! = 2 | | VALUES[2]! = 3| | VALUES[3]! = 4 | | VALUES[4]! = 5{t.error ("QuickSort () failed, Got", Values, "excepted 1 2 3 4 5")}}func TestQuickSort2 (t *testing. T) {values: = []int{5,5,3,2,1}mysort. QuickSort (values) if values[0]! = 1 | | VALUES[1]! = 2 | | VALUES[2]! = 3| | VALUES[3]! = 5 | | VALUES[4]! = 5{t.error ("QuickSort () failed, Got", Values, "excepted 1 2 3 5 5")}}func TestQuickSort3 (t *testing. T) {values: = []int{5}mysort. QuickSort (values) if values[0]! = 5{t.error ("QuickSort () failed, Got", Values, "excepted 5")}}

Run: First Test (with quick sort as an example)

Go to the G:\go\src\sort_test directory

>CD/D G:\go\src\sort_test
>go Install

>go Build Qsort_test.go

>go Test Qsort_test.go

If the code and Gopath settings are fine, then the OK is displayed

Then run the main program, before this, now create a new file "Unsorted.dat" in the SRC directory, open with Notepad and enter

123
3046
3
64
490
1
23
5331
2
7
4
2
132

Save, Next:

Entry: G:\GO\SRC

>CD G:\GO\SRC

>go Build

A sorter.exe is then generated in the SRC directory

Command line Input (G:\GO\SRC):

Sorter.exe-i unsorted.dat-o sorted.dat-a qsort (if you want to run the bubble sort the last word of this line is changed to: Bubblesort)

If the code and Gopath configuration is not a problem, then you will generate a sorted.dat in the SRC directory, open with Notepad to find that one side of the content is already well-ordered


Good The operation ends here--------------------------------------------------------------------------------------------------------


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.