Loading android/onceper.go +1 −1 Original line number Diff line number Diff line Loading @@ -70,7 +70,7 @@ func (once *OncePer) Get(key OnceKey) interface{} { panic(fmt.Errorf("Get() called before Once()")) } return v return once.maybeWaitFor(key, v) } // OnceStringSlice is the same as Once, but returns the value cast to a []string Loading android/onceper_test.go +31 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package android import ( "testing" "time" ) func TestOncePer_Once(t *testing.T) { Loading @@ -34,6 +35,21 @@ func TestOncePer_Once(t *testing.T) { } } func TestOncePer_Once_wait(t *testing.T) { once := OncePer{} key := NewOnceKey("key") ch := make(chan bool) go once.Once(key, func() interface{} { close(ch); time.Sleep(100 * time.Millisecond); return "foo" }) <-ch a := once.Once(key, func() interface{} { return "bar" }).(string) if a != "foo" { t.Errorf("expect %q, got %q", "foo", a) } } func TestOncePer_Get(t *testing.T) { once := OncePer{} key := NewOnceKey("key") Loading Loading @@ -65,6 +81,21 @@ func TestOncePer_Get_panic(t *testing.T) { once.Get(key) } func TestOncePer_Get_wait(t *testing.T) { once := OncePer{} key := NewOnceKey("key") ch := make(chan bool) go once.Once(key, func() interface{} { close(ch); time.Sleep(100 * time.Millisecond); return "foo" }) <-ch a := once.Get(key).(string) if a != "foo" { t.Errorf("expect %q, got %q", "foo", a) } } func TestOncePer_OnceStringSlice(t *testing.T) { once := OncePer{} key := NewOnceKey("key") Loading android/util.go +5 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,11 @@ import ( "strings" ) // CopyOf returns a new slice that has the same contents as s. func CopyOf(s []string) []string { return append([]string(nil), s...) } func JoinWithPrefix(strs []string, prefix string) string { if len(strs) == 0 { return "" Loading android/util_test.go +45 −0 Original line number Diff line number Diff line Loading @@ -15,6 +15,7 @@ package android import ( "fmt" "reflect" "testing" ) Loading Loading @@ -359,3 +360,47 @@ func TestRemoveFromList(t *testing.T) { }) } } func ExampleCopyOf() { a := []string{"1", "2", "3"} b := CopyOf(a) a[0] = "-1" fmt.Printf("a = %q\n", a) fmt.Printf("b = %q\n", b) // Output: // a = ["-1" "2" "3"] // b = ["1" "2" "3"] } func ExampleCopyOf_append() { a := make([]string, 1, 2) a[0] = "foo" fmt.Println("Without CopyOf:") b := append(a, "bar") c := append(a, "baz") fmt.Printf("a = %q\n", a) fmt.Printf("b = %q\n", b) fmt.Printf("c = %q\n", c) a = make([]string, 1, 2) a[0] = "foo" fmt.Println("With CopyOf:") b = append(CopyOf(a), "bar") c = append(CopyOf(a), "baz") fmt.Printf("a = %q\n", a) fmt.Printf("b = %q\n", b) fmt.Printf("c = %q\n", c) // Output: // Without CopyOf: // a = ["foo"] // b = ["foo" "baz"] // c = ["foo" "baz"] // With CopyOf: // a = ["foo"] // b = ["foo" "bar"] // c = ["foo" "baz"] } dexpreopt/dexpreopt.go +1 −3 Original line number Diff line number Diff line Loading @@ -578,9 +578,7 @@ func replace(l []string, from, to string) { } } func copyOf(l []string) []string { return append([]string(nil), l...) } var copyOf = android.CopyOf func anyHavePrefix(l []string, prefix string) bool { for _, x := range l { Loading Loading
android/onceper.go +1 −1 Original line number Diff line number Diff line Loading @@ -70,7 +70,7 @@ func (once *OncePer) Get(key OnceKey) interface{} { panic(fmt.Errorf("Get() called before Once()")) } return v return once.maybeWaitFor(key, v) } // OnceStringSlice is the same as Once, but returns the value cast to a []string Loading
android/onceper_test.go +31 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package android import ( "testing" "time" ) func TestOncePer_Once(t *testing.T) { Loading @@ -34,6 +35,21 @@ func TestOncePer_Once(t *testing.T) { } } func TestOncePer_Once_wait(t *testing.T) { once := OncePer{} key := NewOnceKey("key") ch := make(chan bool) go once.Once(key, func() interface{} { close(ch); time.Sleep(100 * time.Millisecond); return "foo" }) <-ch a := once.Once(key, func() interface{} { return "bar" }).(string) if a != "foo" { t.Errorf("expect %q, got %q", "foo", a) } } func TestOncePer_Get(t *testing.T) { once := OncePer{} key := NewOnceKey("key") Loading Loading @@ -65,6 +81,21 @@ func TestOncePer_Get_panic(t *testing.T) { once.Get(key) } func TestOncePer_Get_wait(t *testing.T) { once := OncePer{} key := NewOnceKey("key") ch := make(chan bool) go once.Once(key, func() interface{} { close(ch); time.Sleep(100 * time.Millisecond); return "foo" }) <-ch a := once.Get(key).(string) if a != "foo" { t.Errorf("expect %q, got %q", "foo", a) } } func TestOncePer_OnceStringSlice(t *testing.T) { once := OncePer{} key := NewOnceKey("key") Loading
android/util.go +5 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,11 @@ import ( "strings" ) // CopyOf returns a new slice that has the same contents as s. func CopyOf(s []string) []string { return append([]string(nil), s...) } func JoinWithPrefix(strs []string, prefix string) string { if len(strs) == 0 { return "" Loading
android/util_test.go +45 −0 Original line number Diff line number Diff line Loading @@ -15,6 +15,7 @@ package android import ( "fmt" "reflect" "testing" ) Loading Loading @@ -359,3 +360,47 @@ func TestRemoveFromList(t *testing.T) { }) } } func ExampleCopyOf() { a := []string{"1", "2", "3"} b := CopyOf(a) a[0] = "-1" fmt.Printf("a = %q\n", a) fmt.Printf("b = %q\n", b) // Output: // a = ["-1" "2" "3"] // b = ["1" "2" "3"] } func ExampleCopyOf_append() { a := make([]string, 1, 2) a[0] = "foo" fmt.Println("Without CopyOf:") b := append(a, "bar") c := append(a, "baz") fmt.Printf("a = %q\n", a) fmt.Printf("b = %q\n", b) fmt.Printf("c = %q\n", c) a = make([]string, 1, 2) a[0] = "foo" fmt.Println("With CopyOf:") b = append(CopyOf(a), "bar") c = append(CopyOf(a), "baz") fmt.Printf("a = %q\n", a) fmt.Printf("b = %q\n", b) fmt.Printf("c = %q\n", c) // Output: // Without CopyOf: // a = ["foo"] // b = ["foo" "baz"] // c = ["foo" "baz"] // With CopyOf: // a = ["foo"] // b = ["foo" "bar"] // c = ["foo" "baz"] }
dexpreopt/dexpreopt.go +1 −3 Original line number Diff line number Diff line Loading @@ -578,9 +578,7 @@ func replace(l []string, from, to string) { } } func copyOf(l []string) []string { return append([]string(nil), l...) } var copyOf = android.CopyOf func anyHavePrefix(l []string, prefix string) bool { for _, x := range l { Loading