Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package main |
| 6 | |
Rob Pike | bdbb3b4 | 2009-03-18 14:09:16 -0700 | [diff] [blame] | 7 | import ( |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 8 | "fmt" |
Rob Pike | e3b6188 | 2010-12-09 16:18:31 -0500 | [diff] [blame] | 9 | "./sort" |
Rob Pike | bdbb3b4 | 2009-03-18 14:09:16 -0700 | [diff] [blame] | 10 | ) |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 11 | |
| 12 | func ints() { |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 13 | data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586} |
| 14 | a := sort.IntArray(data) |
| 15 | sort.Sort(a) |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 16 | if !sort.IsSorted(a) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 17 | panic("fail") |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 18 | } |
| 19 | } |
| 20 | |
| 21 | func strings() { |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 22 | data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"} |
| 23 | a := sort.StringArray(data) |
| 24 | sort.Sort(a) |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 25 | if !sort.IsSorted(a) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 26 | panic("fail") |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | |
Rob Pike | 293c8f8 | 2009-01-15 17:54:07 -0800 | [diff] [blame] | 30 | type day struct { |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 31 | num int |
| 32 | shortName string |
| 33 | longName string |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Rob Pike | 293c8f8 | 2009-01-15 17:54:07 -0800 | [diff] [blame] | 36 | type dayArray struct { |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 37 | data []*day |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 40 | func (p *dayArray) Len() int { return len(p.data) } |
| 41 | func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num } |
| 42 | func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] } |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 43 | |
| 44 | func days() { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 45 | Sunday := day{0, "SUN", "Sunday"} |
| 46 | Monday := day{1, "MON", "Monday"} |
| 47 | Tuesday := day{2, "TUE", "Tuesday"} |
| 48 | Wednesday := day{3, "WED", "Wednesday"} |
| 49 | Thursday := day{4, "THU", "Thursday"} |
| 50 | Friday := day{5, "FRI", "Friday"} |
| 51 | Saturday := day{6, "SAT", "Saturday"} |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 52 | data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday} |
| 53 | a := dayArray{data} |
| 54 | sort.Sort(&a) |
Rob Pike | 40d5435 | 2009-01-09 15:16:31 -0800 | [diff] [blame] | 55 | if !sort.IsSorted(&a) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 56 | panic("fail") |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 57 | } |
Russ Cox | ae54cf7 | 2009-09-15 12:42:24 -0700 | [diff] [blame] | 58 | for _, d := range data { |
Rob Pike | 83f0b71 | 2009-11-01 20:47:03 -0800 | [diff] [blame] | 59 | fmt.Printf("%s ", d.longName) |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 60 | } |
Rob Pike | 78395213 | 2009-10-13 12:37:04 -0700 | [diff] [blame] | 61 | fmt.Printf("\n") |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | func main() { |
Rob Pike | 34356e9 | 2009-12-16 10:29:53 +1100 | [diff] [blame] | 66 | ints() |
| 67 | strings() |
| 68 | days() |
Rob Pike | a43033a | 2008-09-15 11:48:37 -0700 | [diff] [blame] | 69 | } |