本文整理汇总了Golang中sort.IsSorted函数的典型用法代码示例。如果您正苦于以下问题:Golang IsSorted函数的具体用法?Golang IsSorted怎么用?Golang IsSorted使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IsSorted函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: loadPerms
func loadPerms(path string) (mperms PermissionsList) {
file, e := ioutil.ReadFile(path)
if e != nil {
fmt.Println("Could not get group permissions for", path, ":", e)
return
}
lines := bytes.Split(file, []byte("\n"), -1)
mperms = make(PermissionsList, len(lines))
for i, line := range lines {
parts := bytes.Split(line, []byte(" "), 2)
perms := mperms[i]
for _, perm := range parts[0] {
switch perm {
case 'r':
perms.Read = true
case 'w':
perms.Write = true
default:
fmt.Println("WARNING: Unrecognized permission", perm)
}
perms.Path = string(parts[1])
mperms[i] = perms
}
}
sort.Sort(mperms)
if !sort.IsSorted(mperms) {
fmt.Println("Failed to sort!")
}
return
}
示例2: TestUxArraySorting
func TestUxArraySorting(t *testing.T) {
uxa := make(UxArray, 4)
for i := 0; i < len(uxa); i++ {
uxa[i] = makeUxOut(t)
}
isSorted := manualUxArrayIsSorted(uxa)
assert.Equal(t, sort.IsSorted(uxa), isSorted)
assert.Equal(t, uxa.IsSorted(), isSorted)
// Make sure uxa is not sorted
if isSorted {
uxa[0], uxa[1] = uxa[1], uxa[0]
}
assert.False(t, manualUxArrayIsSorted(uxa))
assert.False(t, sort.IsSorted(uxa))
assert.False(t, uxa.IsSorted())
uxb := make(UxArray, 4)
for i, ux := range uxa {
uxb[i] = ux
}
sort.Sort(uxa)
assert.True(t, sort.IsSorted(uxa))
assert.True(t, manualUxArrayIsSorted(uxa))
assert.True(t, uxa.IsSorted())
assert.False(t, sort.IsSorted(uxb))
uxb.Sort()
assert.Equal(t, uxa, uxb)
assert.True(t, sort.IsSorted(uxb))
assert.True(t, manualUxArrayIsSorted(uxb))
assert.True(t, uxb.IsSorted())
}
示例3: runTest
func runTest(t *testing.T, f func(Cover, Cover) Cover, sorted, symmetric bool, tests []Test) {
if symmetric {
for _, test := range tests {
tests = append(tests, Test{test.V1, test.V0, test.R})
}
}
tests = append(tests, Test{Cover{}, Cover{}, Cover{}})
for _, test := range tests {
if sorted {
if !sort.IsSorted(test.V0) {
t.Fatalf("input is not sorted: %+v", test.V0)
}
if !sort.IsSorted(test.V1) {
t.Fatalf("input is not sorted: %+v", test.V1)
}
}
if !sort.IsSorted(test.R) {
t.Fatalf("golden is not sorted: %+v", test.R)
}
res := f(test.V0, test.V1)
if !sort.IsSorted(res) {
t.Fatalf("output is not sorted: %+v", res)
}
if (len(res) != 0 || len(test.R) != 0) && !reflect.DeepEqual(res, test.R) {
t.Fatalf("f(%+v, %+v) = %+v (expect: %+v)", test.V0, test.V1, res, test.R)
}
}
}
示例4: IsSorted
// IsSorted checks whether tx has inputs and outputs sorted according to BIP
// 69.
func IsSorted(tx *wire.MsgTx) bool {
if !sort.IsSorted(sortableInputSlice(tx.TxIn)) {
return false
}
if !sort.IsSorted(sortableOutputSlice(tx.TxOut)) {
return false
}
return true
}
示例5: TestUnitSliceSort
func (s *S) TestUnitSliceSort(c *gocheck.C) {
units := UnitSlice{
Unit{Name: "f", State: provision.StatusBuilding.String()},
Unit{Name: "g", State: provision.StatusStarted.String()},
Unit{Name: "b", State: provision.StatusDown.String()},
}
c.Assert(sort.IsSorted(units), gocheck.Equals, false)
sort.Sort(units)
c.Assert(sort.IsSorted(units), gocheck.Equals, true)
}
示例6: TestcontainerSliceSort
func (S) TestcontainerSliceSort(c *gocheck.C) {
containers := containerSlice{
container{Name: "f", Status: provision.StatusBuilding.String()},
container{Name: "g", Status: provision.StatusStarted.String()},
container{Name: "b", Status: provision.StatusDown.String()},
}
c.Assert(sort.IsSorted(containers), gocheck.Equals, false)
sort.Sort(containers)
c.Assert(sort.IsSorted(containers), gocheck.Equals, true)
}
示例7: TestLookupTableSorted
func TestLookupTableSorted(t *testing.T) {
if !sort.IsSorted(lookupTableSortByCp949{fromTable}) {
t.Error("fromTable is not sorted!")
}
t.Log("fromTable is sorted")
if !sort.IsSorted(lookupTableSortByUcs2{toTable}) {
t.Error("toTable is not sorted!")
}
t.Log("toTable is sorted")
}
示例8: TestUnitSliceSort
func (s *S) TestUnitSliceSort(c *gocheck.C) {
units := UnitSlice{
Unit{Name: "b", State: string(provision.StatusDown)},
Unit{Name: "c", State: string(provision.StatusPending)},
Unit{Name: "a", State: string(provision.StatusError)},
Unit{Name: "d", State: string(provision.StatusCreating)},
Unit{Name: "e", State: string(provision.StatusInstalling)},
Unit{Name: "f", State: string(provision.StatusStarted)},
}
c.Assert(sort.IsSorted(units), gocheck.Equals, false)
sort.Sort(units)
c.Assert(sort.IsSorted(units), gocheck.Equals, true)
}
示例9: TestMergeInt
func TestMergeInt(t *testing.T) {
l := SortedInt(10)
if !sort.IsSorted(int64arr(l)) {
t.Errorf("Not sorted: [%v]\n", l)
t.FailNow()
}
for i := 0; i < 50000; i++ {
l = mergeInt(l, rand.Int63())
if !sort.IsSorted(int64arr(l)) {
t.Errorf("Not sorted: [%v]\n", l)
t.FailNow()
}
}
}
示例10: TestOldestUxOut
func TestOldestUxOut(t *testing.T) {
uxs := OldestUxOut(makeUxArray(t, 4))
for i, _ := range uxs {
uxs[i].Head.BkSeq = uint64(i)
}
assert.True(t, sort.IsSorted(uxs))
assert.Equal(t, uxs.Len(), 4)
uxs.Swap(0, 1)
assert.False(t, sort.IsSorted(uxs))
assert.Equal(t, uxs[0].Head.BkSeq, uint64(1))
assert.Equal(t, uxs[1].Head.BkSeq, uint64(0))
uxs.Swap(0, 1)
assert.True(t, sort.IsSorted(uxs))
assert.Equal(t, uxs[0].Head.BkSeq, uint64(0))
assert.Equal(t, uxs[1].Head.BkSeq, uint64(1))
// Test hash sorting
uxs[1].Head.BkSeq = uint64(0)
h0 := uxs[0].Hash()
h1 := uxs[1].Hash()
firstLower := bytes.Compare(h0[:], h1[:]) < 0
if firstLower {
uxs.Swap(0, 1)
}
assert.False(t, sort.IsSorted(uxs))
sort.Sort(uxs)
cmpHash := false
cmpSeq := false
for i, _ := range uxs[:len(uxs)-1] {
j := i + 1
if uxs[i].Head.BkSeq == uxs[j].Head.BkSeq {
ih := uxs[i].Hash()
jh := uxs[j].Hash()
assert.True(t, bytes.Compare(ih[:], jh[:]) < 0)
cmpHash = true
} else {
assert.True(t, uxs[i].Head.BkSeq < uxs[j].Head.BkSeq)
cmpSeq = true
}
}
assert.True(t, cmpHash)
assert.True(t, cmpSeq)
// Duplicate output panics
uxs = append(uxs, uxs[0])
assert.Panics(t, func() { sort.Sort(uxs) })
}
示例11: TestAdd
func TestAdd(t *testing.T) {
x := New()
x.Add("abcdefg")
checkNum(len(x.circle), 20, t)
checkNum(len(x.sortedHashes), 20, t)
if sort.IsSorted(x.sortedHashes) == false {
t.Errorf("expected sorted hashes to be sorted")
}
x.Add("qwer")
checkNum(len(x.circle), 40, t)
checkNum(len(x.sortedHashes), 40, t)
if sort.IsSorted(x.sortedHashes) == false {
t.Errorf("expected sorted hashes to be sorted")
}
}
示例12: TestSort
func TestSort(t *testing.T) {
assert.Init(t)
func() {
array := make(Array, 0, 10)
assert.Ok(sort.IsSorted(array.Sort()))
}()
func() {
array := Array{1, 5, 9, 8, 7, 6, 2, 4, 3}
assert.Ok(sort.IsSorted(array.Sort()))
}()
}
示例13: Enum
// Enum computes a performance range from a sorted list
// of scores of examples with ground truth labels.
func Enum(vals []ValScore) PerfPath {
if !sort.IsSorted(byScoreDesc(vals)) {
panic("not sorted")
}
// Count number of positive and negative.
var pos, neg int
for _, val := range vals {
if val.Pos {
pos++
} else {
neg++
}
}
// Start with high threshold, everything negative,
// then gradually lower it.
perfs := make([]Perf, 0, len(vals)+1)
perf := Perf{FN: pos, TN: neg}
perfs = append(perfs, perf)
for _, val := range vals {
if val.Pos {
// Positive example classified as positive instead of negative.
perf.TP, perf.FN = perf.TP+1, perf.FN-1
} else {
// Negative example classified as positive instead of negative.
perf.FP, perf.TN = perf.FP+1, perf.TN-1
}
perfs = append(perfs, perf)
}
return PerfPath(perfs)
}
示例14: TestSortFloat64Slice
func TestSortFloat64Slice(t *testing.T) {
data := float64s
a := make(Float64Slice, testSize)
nanCount := 0
for i := range a {
a[i] = data[i%len(data)]
if math.IsNaN(a[i]) {
nanCount++
}
}
a.Sort()
if !sort.IsSorted(a) {
t.Errorf("sorted %v", float64s)
t.Errorf(" got %v", data)
}
// sort.IsSorted will compare using the Key, so compare using < to see if
// Key func is wrong
prev := a[0]
for _, v := range a {
if v < prev {
t.Errorf("Float64Key is wrong: %f sorted before %f", prev, v)
}
prev = v
}
// floats data contains two NaNs, so Search will find the spot right
// before them.
if a.Search(math.Inf(-1)) != 0 || a.Search(math.NaN()) != len(a)-nanCount {
t.Errorf("search failed")
}
}
示例15: Test_SelectionSort
func Test_SelectionSort(t *testing.T) {
ck := getCkints(1000)
SelectionSort(ck)
if !sort.IsSorted(ck) {
t.Error("SelectionSort Error")
}
}