// Data returns a slice containing the elements of iter. func Data(iter Iterable) []interface{} { vec := new(vector.Vector) for e := range iter.Iter() { vec.Push(e) } return vec.Data() }
func TestDecodeFlushElem(t *testing.T) { testVec := new(vector.Vector).Resize(2, 2) nb := newDecoder(testVec, 1) nb.Float64(3.14159) nb.Flush() assertResult(t, testVec.Data(), []interface{}{nil, float64(3.14159)}) }
func TestGroupBy(t *testing.T) { in := IntArray{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5} exp := [][]int{[]int{1}, []int{2, 2}, []int{3, 3, 3}, []int{4, 4, 4, 4}, []int{5, 5, 5, 5, 5}} i := 0 for x := range GroupBy(in, intkey{}).Iter() { gr := x.(Group) if gr.Key.(int) != i+1 { t.Fatal("group key wrong; expected", i+1, "but got", gr.Key.(int)) } vals := Data(gr.Vals) assertArraysAreEqual(t, vals, exp[i]) i++ } if i != 5 { t.Fatal("did not return expected number of groups") } // test 0 length Iterable for _ = range GroupBy(IntArray([]int{}), &intkey{}).Iter() { t.Fatal("iterator should be empty") } // test case with only uniques var out vector.Vector for x := range GroupBy(elevenToTwenty, intkey{}).Iter() { out.Push(x.(Group).Key) } assertArraysAreEqual(t, out.Data(), elevenToTwenty) }
func Respond(query types.DNSquery) (result types.DNSresponse) { result.Responsecode = types.SERVFAIL qname := strings.ToLower(query.Qname) var asection, nssection vector.Vector if query.Qclass == types.IN { if as112Domain.MatchString(qname) { result.Responsecode = types.NOERROR if types.QMatches(query.Qtype, types.NS) { nsRecords(query.Qname, &asection) } if types.QMatches(query.Qtype, types.SOA) { asection.Push(soaRecord(query.Qname, as112soa)) } } matches := as112SubDomain.MatchStrings(qname) if len(matches) > 0 { result.Responsecode = types.NXDOMAIN nssection.Push(soaRecord(matches[1], as112soa)) } if qname == "hostname.as112.net" { result.Responsecode = types.NOERROR if types.QMatches(query.Qtype, types.TXT) { for i := 0; i < len(hostnameAnswers); i++ { asection.Push(types.RR{ Name: query.Qname, TTL: defaultTTL, Type: types.TXT, Class: types.IN, Data: types.ToTXT(hostnameAnswers[i]), }) } } if types.QMatches(query.Qtype, types.NS) { nsRecords(query.Qname, &asection) } if types.QMatches(query.Qtype, types.SOA) { asection.Push(soaRecord(query.Qname, hostnamesoa)) } } } data := asection.Data() result.Asection = make([]types.RR, len(data)) for i := 0; i < len(data); i++ { result.Asection[i] = data[i].(types.RR) } data = nssection.Data() result.Nssection = make([]types.RR, len(data)) for i := 0; i < len(data); i++ { result.Nssection[i] = data[i].(types.RR) } return }
func (self *quantifiedExpr) Match(m Position) (Position, interface{}) { var item interface{} cur := m res := new(vector.Vector) // guaranteed minimum for i := 0; i < self.min; i++ { cur, item = self.e.Match(cur) if cur.Failed() { return cur, nil } res.Push(item) } last := cur // optional (up to a maximum) for i := self.min; self.max == -1 || i < self.max; i++ { cur, item = self.e.Match(last) if cur.Failed() { return last, res.Data() } res.Push(item) last = cur } return cur, res.Data() }