This is an automated email from the ASF dual-hosted git repository.
placave pushed a commit to branch cpc-sketch
in repository https://gitbox.apache.org/repos/asf/datasketches-go.git
The following commit(s) were added to refs/heads/cpc-sketch by this push:
new 04e51ea Add sanity test for cpc union
04e51ea is described below
commit 04e51ea1586a0a87371baee010dcaeb8a3f1a62b
Author: Pierre Lacave <[email protected]>
AuthorDate: Wed Dec 18 12:02:28 2024 +0100
Add sanity test for cpc union
---
cpc/cpc_sketch.go | 4 ++--
cpc/cpc_sketch_test.go | 2 +-
cpc/cpc_union.go | 11 +++++++++
cpc/cpc_union_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
internal/family.go | 5 ++++
5 files changed, 82 insertions(+), 3 deletions(-)
diff --git a/cpc/cpc_sketch.go b/cpc/cpc_sketch.go
index 1b6eba4..156b82d 100644
--- a/cpc/cpc_sketch.go
+++ b/cpc/cpc_sketch.go
@@ -52,8 +52,8 @@ type CpcSketch struct {
scratch [8]byte
}
-func NewCpcSketchWithDefault() (CpcSketch, error) {
- return NewCpcSketch(defaultLgK, internal.DEFAULT_UPDATE_SEED)
+func NewCpcSketchWithDefault(lgK int) (CpcSketch, error) {
+ return NewCpcSketch(lgK, internal.DEFAULT_UPDATE_SEED)
}
func NewCpcSketch(lgK int, seed uint64) (CpcSketch, error) {
diff --git a/cpc/cpc_sketch_test.go b/cpc/cpc_sketch_test.go
index 1841e99..99345db 100644
--- a/cpc/cpc_sketch_test.go
+++ b/cpc/cpc_sketch_test.go
@@ -126,7 +126,7 @@ func TestCPCCheckLgK(t *testing.T) {
assert.Equal(t, sk.lgK, 10)
_, err = NewCpcSketch(3, 0)
assert.Error(t, err)
- sk, err = NewCpcSketchWithDefault()
+ sk, err = NewCpcSketchWithDefault(defaultLgK)
assert.NoError(t, err)
assert.Equal(t, sk.lgK, defaultLgK)
assert.Equal(t, sk.seed, internal.DEFAULT_UPDATE_SEED)
diff --git a/cpc/cpc_union.go b/cpc/cpc_union.go
index 933ca81..99396bd 100644
--- a/cpc/cpc_union.go
+++ b/cpc/cpc_union.go
@@ -54,6 +54,10 @@ func NewCpcUnionSketchWithDefault(lgK int) (CpcUnion, error)
{
return NewCpcUnionSketch(lgK, internal.DEFAULT_UPDATE_SEED)
}
+func (u *CpcUnion) GetFamilyId() int {
+ return internal.FamilyEnum.CPC.Id
+}
+
func (u *CpcUnion) Update(source CpcSketch) error {
if err := checkSeeds(u.seed, source.seed); err != nil {
return err
@@ -382,3 +386,10 @@ func (u *CpcUnion) orMatrixIntoMatrix(srcMatrix []uint64,
srcLgK int) {
}
}
+
+func (u *CpcUnion) getNumCoupons() uint64 {
+ if u.bitMatrix != nil {
+ return countBitsSetInMatrix(u.bitMatrix)
+ }
+ return u.accumulator.numCoupons
+}
diff --git a/cpc/cpc_union_test.go b/cpc/cpc_union_test.go
new file mode 100644
index 0000000..dab028b
--- /dev/null
+++ b/cpc/cpc_union_test.go
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package cpc
+
+import (
+ "github.com/apache/datasketches-go/internal"
+ "github.com/stretchr/testify/assert"
+ "testing"
+)
+
+func TestCpcUnionError(t *testing.T) {
+ sk, err := NewCpcSketch(10, 1)
+ union, err := NewCpcUnionSketchWithDefault(defaultLgK)
+ err = union.Update(sk)
+ if err == nil {
+ t.Errorf("Expected error")
+ }
+}
+
+func TestCpcGetters(t *testing.T) {
+ lgK := 10
+ union, err := NewCpcUnionSketchWithDefault(lgK)
+ if err != nil {
+ t.Errorf("Error: %s", err)
+ }
+ assert.Equal(t, union.lgK, lgK)
+ assert.Equal(t, union.getNumCoupons(), uint64(0))
+ sk, err := NewCpcSketch(lgK, internal.DEFAULT_UPDATE_SEED)
+ assert.NoError(t, err)
+ for i := 0; i <= (4 << lgK); i++ {
+ err := sk.UpdateInt64(int64(i))
+ assert.NoError(t, err)
+ }
+ err = union.Update(sk)
+ assert.NoError(t, err)
+ assert.True(t, union.getNumCoupons() > 0)
+ assert.NotNil(t, getBitMatrix(union))
+ assert.Equal(t, union.GetFamilyId(), internal.FamilyEnum.CPC.Id)
+
+}
+
+func getBitMatrix(union CpcUnion) []uint64 {
+ //checkUnionState(union)
+ if union.bitMatrix != nil {
+ return union.bitMatrix
+ }
+ return bitMatrixOfSketch(union.accumulator)
+}
diff --git a/internal/family.go b/internal/family.go
index 33bc19b..0a43a30 100644
--- a/internal/family.go
+++ b/internal/family.go
@@ -26,6 +26,7 @@ type families struct {
HLL family
Frequency family
Kll family
+ CPC family
}
var FamilyEnum = &families{
@@ -41,4 +42,8 @@ var FamilyEnum = &families{
Id: 15,
MaxPreLongs: 2,
},
+ CPC: family{
+ Id: 16,
+ MaxPreLongs: 5,
+ },
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]