CodeSampleX

示例

google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5: distribution.Distribution

已验证示例 — golang google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5: distribution.Distribution. contract 在 go 1.26 · linux debian/x64…

sha256:40cadfb768e0067dcd313aa207008330ba3604170e9cee70311e1b0e116e4f07

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-05

案例

HOW
目标
verify distribution.Distribution in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5
包
符号
  • distribution.Distribution
创建时间
2026-09-05T12:01:10Z

契约

  1. Distribution zero value has Count=0, Mean=0, and nil Range
  2. NewDistribution constructs a Distribution with specified count, mean, sum of squared deviations, and range
  3. GetRange returns min and max bounds for population values
  4. SetLinearBuckets configures linear bucket options with finite buckets, width, and offset
  5. TotalBucketCount calculates sum of bucket counts matching the total population count
  6. Distribution ProtoReflect returns non-nil protobuf Message descriptor
  7. Distribution String returns formatted text representation

文件

  • PROMPT.md
  • csx.json
  • distribution.go
  • go.mod
  • go.sum
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify distribution.Distribution in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5
Demonstrate these symbols/APIs:
  - distribution.Distribution

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:638fa94e6b2954145469cb8ac2e9b652bfdb7b2466e0cbf70bda67f5c11d4bdb","contract":["Distribution zero value has Count=0, Mean=0, and nil Range","NewDistribution constructs a Distribution with specified count, mean, sum of squared deviations, and range","GetRange returns min and max bounds for population values","SetLinearBuckets configures linear bucket options with finite buckets, width, and offset","TotalBucketCount calculates sum of bucket counts matching the total population count","Distribution ProtoReflect returns non-nil protobuf Message descriptor","Distribution String returns formatted text representation"],"goal":"verify distribution.Distribution in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5","kind":"HOW","packages":["pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5"],"schemaVersion":1,"symbols":["distribution.Distribution"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5","symbols":["distribution.Distribution"],"verifierAdapter":"golang@1"}
distribution.go
package sample

import (
	"google.golang.org/genproto/googleapis/api/distribution"
)

// NewDistribution creates a Distribution with basic population statistics and range.
func NewDistribution(count int64, mean, sumSqDev, min, max float64) *distribution.Distribution {
	return &distribution.Distribution{
		Count:                 count,
		Mean:                  mean,
		SumOfSquaredDeviation: sumSqDev,
		Range: &distribution.Distribution_Range{
			Min: min,
			Max: max,
		},
	}
}

// SetLinearBuckets configures linear bucket options and bucket counts on a Distribution.
func SetLinearBuckets(d *distribution.Distribution, numFiniteBuckets int32, width, offset float64, counts []int64) {
	d.BucketOptions = &distribution.Distribution_BucketOptions{
		Options: &distribution.Distribution_BucketOptions_LinearBuckets{
			LinearBuckets: &distribution.Distribution_BucketOptions_Linear{
				NumFiniteBuckets: numFiniteBuckets,
				Width:            width,
				Offset:           offset,
			},
		},
	}
	d.BucketCounts = counts
}

// TotalBucketCount sums the counts across all histogram buckets.
func TotalBucketCount(d *distribution.Distribution) int64 {
	var total int64
	for _, c := range d.GetBucketCounts() {
		total += c
	}
	return total
}

// HasHistogram returns true if bucket options and bucket counts are present.
func HasHistogram(d *distribution.Distribution) bool {
	return d.GetBucketOptions() != nil && len(d.GetBucketCounts()) > 0
}
go.mod
module sample

go 1.26.6

require google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5

require google.golang.org/protobuf v1.36.12 // indirect
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5 h1:izFU9hz7aeLI/Mi1J0991ae+xcwRLr7hTqWnB/9aIIU=
google.golang.org/genproto/googleapis/api v0.0.0-20260825221802-da73d73af1c5/go.mod h1:3LhxRw4YYkf+ylAfgaY9JlVLFKhokkCV8duhLLe7+t0=
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify distribution.Distribution in pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/genproto/googleapis/api@v0.0.0-20260825221802-da73d73af1c5"
  ],
  "symbols": [
    "distribution.Distribution"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"google.golang.org/genproto/googleapis/api/distribution"
	"sample"
)

func main() {
	// Assertion 1: Distribution zero value has Count=0, Mean=0, and nil Range
	var zeroDist distribution.Distribution
	if zeroDist.GetCount() != 0 {
		fmt.Fprintf(os.Stderr, "expected zeroDist.GetCount() == 0, got %d\n", zeroDist.GetCount())
		os.Exit(1)
	}
	if zeroDist.GetMean() != 0 {
		fmt.Fprintf(os.Stderr, "expected zeroDist.GetMean() == 0, got %f\n", zeroDist.GetMean())
		os.Exit(1)
	}
	if zeroDist.GetRange() != nil {
		fmt.Fprintf(os.Stderr, "expected zeroDist.GetRange() to be nil\n")
		os.Exit(1)
	}

	// Assertion 2: NewDistribution constructs a Distribution with specified count, mean, sum of squared deviations, and range
	d := sample.NewDistribution(10, 5.5, 12.25, 1.0, 10.0)
	if d.GetCount() != 10 {
		fmt.Fprintf(os.Stderr, "expected count 10, got %d\n", d.GetCount())
		os.Exit(1)
	}
	if d.GetMean() != 5.5 {
		fmt.Fprintf(os.Stderr, "expected mean 5.5, got %f\n", d.GetMean())
		os.Exit(1)
	}
	if d.GetSumOfSquaredDeviation() != 12.25 {
		fmt.Fprintf(os.Stderr, "expected sum of squared deviation 12.25, got %f\n", d.GetSumOfSquaredDeviation())
		os.Exit(1)
	}

	// Assertion 3: GetRange returns min and max bounds for population values
	r := d.GetRange()
	if r == nil || r.GetMin() != 1.0 || r.GetMax() != 10.0 {
		fmt.Fprintf(os.Stderr, "expected range min 1.0 and max 10.0, got %+v\n", r)
		os.Exit(1)
	}

	// Assertion 4: SetLinearBuckets configures linear bucket options with finite buckets, width, and offset
	counts := []int64{1, 3, 4, 2}
	sample.SetLinearBuckets(d, 2, 5.0, 0.0, counts)
	bo := d.GetBucketOptions()
	if bo == nil {
		fmt.Fprintf(os.Stderr, "expected non-nil BucketOptions\n")
		os.Exit(1)
	}
	linear := bo.GetLinearBuckets()
	if linear == nil || linear.NumFiniteBuckets != 2 || linear.Width != 5.0 || linear.Offset != 0.0 {
		fmt.Fprintf(os.Stderr, "linear bucket mismatch: %+v\n", linear)
		os.Exit(1)
	}

	// Assertion 5: TotalBucketCount calculates sum of bucket counts matching the total population count
	if !sample.HasHistogram(d) {
		fmt.Fprintf(os.Stderr, "expected HasHistogram(d) to be true\n")
		os.Exit(1)
	}
	if total := sample.TotalBucketCount(d); total != 10 {
		fmt.Fprintf(os.Stderr, "expected TotalBucketCount to equal 10, got %d\n", total)
		os.Exit(1)
	}

	// Assertion 6: Distribution ProtoReflect returns non-nil protobuf Message descriptor
	msg := d.ProtoReflect()
	if msg == nil || msg.Descriptor() == nil || msg.Descriptor().FullName() != "google.api.Distribution" {
		fmt.Fprintf(os.Stderr, "unexpected descriptor: %v\n", msg)
		os.Exit(1)
	}

	// Assertion 7: Distribution String returns formatted text representation
	str := d.String()
	if len(str) == 0 {
		fmt.Fprintf(os.Stderr, "expected non-empty string representation\n")
		os.Exit(1)
	}

	fmt.Println("All distribution.Distribution contract assertions passed.")
}

原始种子者

匿名