CodeSampleX

示例

github.com/google/go-cmp v0.5.7: cmp.Equal

已验证示例 — golang github.com/google/go-cmp v0.5.7: cmp.Equal. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: cmp.Equal returns true for identical…

sha256:b829e03598baf4ae5bd6b85b9d412f2673e74ad5d6c4ae435374219a48117ddc

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

执行证据

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

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go 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-11-05

案例

HOW
目标
verify github.com/google/go-cmp/cmp.Equal in pkg:golang/github.com/google/go-cmp@v0.5.7
包
符号
  • github.com/google/go-cmp/cmp.Equal
创建时间
2026-11-05T09:54:56Z

契约

  1. cmp.Equal returns true for identical primitive, slice, and map values and false for differences
  2. cmp.Equal returns true for identical structs with exported fields and false for struct field differences
  3. cmp.Equal supports cmp.Comparer for custom equality predicates
  4. cmpopts.IgnoreFields excludes specified struct fields from comparison in cmp.Equal
  5. cmpopts.SortSlices compares unordered slices in cmp.Equal
  6. cmpopts.IgnoreUnexported and cmp.AllowUnexported handle structs with unexported fields in cmp.Equal
  7. cmp.Transformer applies value transformations before comparison in cmp.Equal
  8. cmpopts.EquateEmpty treats nil and empty slices or maps as equal in cmp.Equal

文件

  • PROMPT.md
  • csx.json
  • 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 github.com/google/go-cmp/cmp.Equal in pkg:golang/github.com/google/go-cmp@v0.5.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/go-cmp@v0.5.7
Demonstrate these symbols/APIs:
  - github.com/google/go-cmp/cmp.Equal

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:dfa59f86f2b5b721a3dbe897a3d6186720ef3c58b5a0f442d8781693d68a51f5","contract":["cmp.Equal returns true for identical primitive, slice, and map values and false for differences","cmp.Equal returns true for identical structs with exported fields and false for struct field differences","cmp.Equal supports cmp.Comparer for custom equality predicates","cmpopts.IgnoreFields excludes specified struct fields from comparison in cmp.Equal","cmpopts.SortSlices compares unordered slices in cmp.Equal","cmpopts.IgnoreUnexported and cmp.AllowUnexported handle structs with unexported fields in cmp.Equal","cmp.Transformer applies value transformations before comparison in cmp.Equal","cmpopts.EquateEmpty treats nil and empty slices or maps as equal in cmp.Equal"],"goal":"verify github.com/google/go-cmp/cmp.Equal in pkg:golang/github.com/google/go-cmp@v0.5.7","kind":"HOW","packages":["pkg:golang/github.com/google/go-cmp@v0.5.7"],"schemaVersion":1,"symbols":["github.com/google/go-cmp/cmp.Equal"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/go-cmp@v0.5.7"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/go-cmp@v0.5.7","symbols":["github.com/google/go-cmp/cmp.Equal"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22

require github.com/google/go-cmp v0.5.7

require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
go.sum
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/google/go-cmp/cmp.Equal in pkg:golang/github.com/google/go-cmp@v0.5.7",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/go-cmp@v0.5.7"
  ],
  "symbols": [
    "github.com/google/go-cmp/cmp.Equal"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

type accountRecord struct {
	ID        int
	Handle    string
	UpdatedAt string
}

type privateHolder struct {
	Label  string
	hidden string
}

func main() {
	if err := runContractTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract tests passed.")
}

func runContractTests() error {
	// 1. cmp.Equal returns true for identical primitive, slice, and map values and false for differences
	if !cmp.Equal(100, 100) {
		return fmt.Errorf("expected 100 == 100")
	}
	if cmp.Equal(100, 101) {
		return fmt.Errorf("expected 100 != 101")
	}
	if !cmp.Equal([]string{"first", "second"}, []string{"first", "second"}) {
		return fmt.Errorf("expected equal string slices")
	}
	if cmp.Equal([]string{"first", "second"}, []string{"second", "first"}) {
		return fmt.Errorf("expected ordered slices to differ by default")
	}
	if !cmp.Equal(map[string]int{"k1": 10, "k2": 20}, map[string]int{"k2": 20, "k1": 10}) {
		return fmt.Errorf("expected map key/value equality")
	}

	// 2. cmp.Equal returns true for identical structs with exported fields and false for struct field differences
	a1 := accountRecord{ID: 1, Handle: "user_one", UpdatedAt: "2026-01-01"}
	a2 := accountRecord{ID: 1, Handle: "user_one", UpdatedAt: "2026-01-01"}
	a3 := accountRecord{ID: 1, Handle: "user_two", UpdatedAt: "2026-01-01"}
	if !cmp.Equal(a1, a2) {
		return fmt.Errorf("expected identical structs to be equal")
	}
	if cmp.Equal(a1, a3) {
		return fmt.Errorf("expected structs with differing fields to be unequal")
	}

	// 3. cmp.Equal supports cmp.Comparer for custom equality predicates
	approxFloat := cmp.Comparer(func(x, y float64) bool {
		diff := x - y
		if diff < 0 {
			diff = -diff
		}
		return diff < 0.001
	})
	if !cmp.Equal(2.0001, 2.0002, approxFloat) {
		return fmt.Errorf("expected custom comparer to treat close floats as equal")
	}
	if cmp.Equal(2.0, 3.0, approxFloat) {
		return fmt.Errorf("expected custom comparer to treat distinct floats as unequal")
	}

	// 4. cmpopts.IgnoreFields excludes specified struct fields from comparison in cmp.Equal
	a4 := accountRecord{ID: 2, Handle: "user_three", UpdatedAt: "2026-01-01"}
	a5 := accountRecord{ID: 2, Handle: "user_three", UpdatedAt: "2026-06-15"}
	if cmp.Equal(a4, a5) {
		return fmt.Errorf("expected structs to differ without IgnoreFields")
	}
	if !cmp.Equal(a4, a5, cmpopts.IgnoreFields(accountRecord{}, "UpdatedAt")) {
		return fmt.Errorf("expected structs to match with UpdatedAt ignored")
	}

	// 5. cmpopts.SortSlices compares unordered slices in cmp.Equal
	s1 := []int{9, 3, 7, 1}
	s2 := []int{1, 3, 7, 9}
	lessInt := func(a, b int) bool { return a < b }
	if cmp.Equal(s1, s2) {
		return fmt.Errorf("expected unordered slices to differ by default")
	}
	if !cmp.Equal(s1, s2, cmpopts.SortSlices(lessInt)) {
		return fmt.Errorf("expected SortSlices to match elements regardless of order")
	}

	// 6. cmpopts.IgnoreUnexported and cmp.AllowUnexported handle structs with unexported fields in cmp.Equal
	p1 := privateHolder{Label: "item", hidden: "val-1"}
	p2 := privateHolder{Label: "item", hidden: "val-2"}
	if !cmp.Equal(p1, p2, cmpopts.IgnoreUnexported(privateHolder{})) {
		return fmt.Errorf("expected IgnoreUnexported to match structs differing only in unexported fields")
	}
	p3 := privateHolder{Label: "item", hidden: "val-1"}
	if !cmp.Equal(p1, p3, cmp.AllowUnexported(privateHolder{})) {
		return fmt.Errorf("expected AllowUnexported to match identical structs with unexported fields")
	}

	// 7. cmp.Transformer applies value transformations before comparison in cmp.Equal
	foldCase := cmp.Transformer("ToLower", strings.ToLower)
	if !cmp.Equal("SampleText", "sampletext", foldCase) {
		return fmt.Errorf("expected transformer to normalize casing")
	}

	// 8. cmpopts.EquateEmpty treats nil and empty slices or maps as equal in cmp.Equal
	var nilSlice []string
	emptySlice := []string{}
	if cmp.Equal(nilSlice, emptySlice) {
		return fmt.Errorf("expected nil slice and empty slice to differ by default")
	}
	if !cmp.Equal(nilSlice, emptySlice, cmpopts.EquateEmpty()) {
		return fmt.Errorf("expected EquateEmpty to treat nil and empty slice as equal")
	}

	return nil
}

原始种子者

匿名