CodeSampleX

示例

github.com/beorn7/perks v1.0.1: quantile.Samples

已验证示例 — golang github.com/beorn7/perks v1.0.1: quantile.Samples. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: quantile.Samples represents a slice…

sha256:82a944be7a5f9d6be7df33c67e0e20238a88d4d590a2869e0158c5bf6b5c5446

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-06

案例

HOW
目标
verify github.com/beorn7/perks/quantile.Samples in pkg:golang/github.com/beorn7/perks@v1.0.1
包
符号
  • github.com/beorn7/perks/quantile.Samples
创建时间
2026-09-06T04:38:22Z

契约

  1. quantile.Samples represents a slice of Sample items containing Value, Width, and Delta
  2. quantile.Samples implements sort.Interface methods Len, Less, and Swap
  3. sort.Sort reorders quantile.Samples in ascending order by Value
  4. sort.IsSorted validates sorted order of quantile.Samples
  5. Stream.Samples returns a quantile.Samples slice representing retained stream observations

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • 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 github.com/beorn7/perks/quantile.Samples in pkg:golang/github.com/beorn7/perks@v1.0.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/beorn7/perks@v1.0.1
Demonstrate these symbols/APIs:
  - github.com/beorn7/perks/quantile.Samples

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:5dbaa8ded5387644f2e179bd90544a22926e005b073e7c97555eefeaf3b45977","contract":["quantile.Samples represents a slice of Sample items containing Value, Width, and Delta","quantile.Samples implements sort.Interface methods Len, Less, and Swap","sort.Sort reorders quantile.Samples in ascending order by Value","sort.IsSorted validates sorted order of quantile.Samples","Stream.Samples returns a quantile.Samples slice representing retained stream observations"],"goal":"verify github.com/beorn7/perks/quantile.Samples in pkg:golang/github.com/beorn7/perks@v1.0.1","kind":"HOW","packages":["pkg:golang/github.com/beorn7/perks@v1.0.1"],"schemaVersion":1,"symbols":["github.com/beorn7/perks/quantile.Samples"]},"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/github.com/beorn7/perks@v1.0.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/beorn7/perks@v1.0.1","symbols":["github.com/beorn7/perks/quantile.Samples"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/beorn7/perks v1.0.1
go.sum
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
main.go
package main

import (
	"fmt"
	"sort"

	"github.com/beorn7/perks/quantile"
)

func main() {
	// Construct a quantile.Samples slice directly
	samples := quantile.Samples{
		{Value: 42.0, Width: 1.0, Delta: 0.0},
		{Value: 10.5, Width: 1.0, Delta: 0.0},
		{Value: 99.9, Width: 2.0, Delta: 0.0},
		{Value: 3.14, Width: 1.0, Delta: 0.0},
	}

	fmt.Printf("Initial samples count: %d\n", samples.Len())
	fmt.Printf("Is sorted initially: %v\n", sort.IsSorted(samples))

	// Sort samples using standard library sort.Sort
	sort.Sort(samples)
	fmt.Printf("Is sorted after sort.Sort: %v\n", sort.IsSorted(samples))
	for i, s := range samples {
		fmt.Printf("Sample %d: Value=%.2f Width=%.1f Delta=%.1f\n", i, s.Value, s.Width, s.Delta)
	}

	// Obtain quantile.Samples from an active Stream
	stream := quantile.NewTargeted(map[float64]float64{0.50: 0.01, 0.90: 0.01})
	for i := 1; i <= 100; i++ {
		stream.Insert(float64(i))
	}

	streamSamples := stream.Samples()
	fmt.Printf("Stream retained samples count: %d\n", streamSamples.Len())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/beorn7/perks/quantile.Samples in pkg:golang/github.com/beorn7/perks@v1.0.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/beorn7/perks@v1.0.1"
  ],
  "symbols": [
    "github.com/beorn7/perks/quantile.Samples"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"sort"

	"github.com/beorn7/perks/quantile"
)

func main() {
	// Assertion 1: quantile.Samples represents a slice of Sample items containing Value, Width, and Delta
	{
		s := quantile.Samples{
			{Value: 15.5, Width: 1.0, Delta: 0.0},
			{Value: 3.2, Width: 2.0, Delta: 0.5},
		}
		if len(s) != 2 {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected length 2, got %d\n", len(s))
			os.Exit(1)
		}
		if s[0].Value != 15.5 || s[0].Width != 1.0 || s[0].Delta != 0.0 {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: unexpected sample values at index 0\n")
			os.Exit(1)
		}
		if s[1].Value != 3.2 || s[1].Width != 2.0 || s[1].Delta != 0.5 {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: unexpected sample values at index 1\n")
			os.Exit(1)
		}
	}

	// Assertion 2: quantile.Samples implements sort.Interface methods Len, Less, and Swap
	{
		var _ sort.Interface = quantile.Samples{}
		s := quantile.Samples{
			{Value: 50.0, Width: 1.0, Delta: 0.0},
			{Value: 20.0, Width: 1.0, Delta: 0.0},
			{Value: 80.0, Width: 1.0, Delta: 0.0},
		}
		if s.Len() != 3 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected Len() == 3, got %d\n", s.Len())
			os.Exit(1)
		}
		if s.Less(0, 1) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected Less(0, 1) to be false (50 < 20)\n")
			os.Exit(1)
		}
		if !s.Less(1, 0) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected Less(1, 0) to be true (20 < 50)\n")
			os.Exit(1)
		}
		s.Swap(0, 1)
		if s[0].Value != 20.0 || s[1].Value != 50.0 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: Swap did not swap elements correctly\n")
			os.Exit(1)
		}
	}

	// Assertion 3: sort.Sort reorders quantile.Samples in ascending order by Value
	{
		s := quantile.Samples{
			{Value: 45.0, Width: 1.0, Delta: 0.0},
			{Value: 12.0, Width: 2.0, Delta: 0.1},
			{Value: 99.0, Width: 1.0, Delta: 0.0},
			{Value: 5.5, Width: 1.0, Delta: 0.0},
			{Value: 33.3, Width: 3.0, Delta: 0.2},
		}
		sort.Sort(s)
		for i := 1; i < len(s); i++ {
			if s[i-1].Value > s[i].Value {
				fmt.Fprintf(os.Stderr, "assertion 3 failed: not sorted at index %d (%f > %f)\n", i, s[i-1].Value, s[i].Value)
				os.Exit(1)
			}
		}
		if s[0].Value != 5.5 || s[len(s)-1].Value != 99.0 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: unexpected min/max after sort: min=%f, max=%f\n", s[0].Value, s[len(s)-1].Value)
			os.Exit(1)
		}
	}

	// Assertion 4: sort.IsSorted validates sorted order of quantile.Samples
	{
		unsorted := quantile.Samples{
			{Value: 10.0},
			{Value: 5.0},
		}
		if sort.IsSorted(unsorted) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: unsorted samples reported as sorted\n")
			os.Exit(1)
		}
		sorted := quantile.Samples{
			{Value: 1.0},
			{Value: 2.0},
			{Value: 3.0},
		}
		if !sort.IsSorted(sorted) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: sorted samples reported as unsorted\n")
			os.Exit(1)
		}
	}

	// Assertion 5: Stream.Samples returns a quantile.Samples slice representing retained stream observations
	{
		stream := quantile.NewTargeted(map[float64]float64{0.50: 0.01})
		for i := 1; i <= 200; i++ {
			stream.Insert(float64(i))
		}
		var samples quantile.Samples = stream.Samples()
		if len(samples) == 0 {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: expected non-empty samples from stream\n")
			os.Exit(1)
		}
		for _, sample := range samples {
			if sample.Value < 1.0 || sample.Value > 200.0 {
				fmt.Fprintf(os.Stderr, "assertion 5 failed: sample value %f out of range [1, 200]\n", sample.Value)
				os.Exit(1)
			}
			if sample.Width <= 0 {
				fmt.Fprintf(os.Stderr, "assertion 5 failed: expected positive sample width, got %f\n", sample.Width)
				os.Exit(1)
			}
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

原始种子者

匿名