CodeSampleX

示例

github.com/hashicorp/golang-lru/v2 v2.0.7: New

已验证示例 — golang github.com/hashicorp/golang-lru/v2 v2.0.7: New. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: lru.New returns a non-nil cache and nil…

sha256:9fb5545ee737b09180bce361a3ccce48f9f82a3f1df75c359783884482bac314

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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-16

案例

HOW
目标
verify github.com/hashicorp/golang-lru/v2.New in pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7
包
符号
  • github.com/hashicorp/golang-lru/v2.New
环境
go 1.26.6
创建时间
2026-09-16T11:18:58Z

契约

  1. lru.New returns a non-nil cache and nil error when initialized with a positive capacity
  2. lru.New returns an error when initialized with zero or negative capacity
  3. Add inserts new key-value pairs, updates existing keys without increasing length, and reports whether an eviction occurred
  4. Get retrieves stored values for present keys and updates recent-ness so that least recently used entries are evicted first upon exceeding capacity
  5. Contains and Peek inspect presence and values without altering LRU order, and Purge removes all entries from the cache

文件

  • 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/hashicorp/golang-lru/v2.New in pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7
Demonstrate these symbols/APIs:
  - github.com/hashicorp/golang-lru/v2.New

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:675eaff6fb3f00f7e288c202ff69c20727d268c532ef942b7035fe23d062c74e","contract":["lru.New returns a non-nil cache and nil error when initialized with a positive capacity","lru.New returns an error when initialized with zero or negative capacity","Add inserts new key-value pairs, updates existing keys without increasing length, and reports whether an eviction occurred","Get retrieves stored values for present keys and updates recent-ness so that least recently used entries are evicted first upon exceeding capacity","Contains and Peek inspect presence and values without altering LRU order, and Purge removes all entries from the cache"],"goal":"verify github.com/hashicorp/golang-lru/v2.New in pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7","kind":"HOW","packages":["pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7"],"schemaVersion":1,"symbols":["github.com/hashicorp/golang-lru/v2.New"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7"],"schemaVersion":1,"subject":"pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7","symbols":["github.com/hashicorp/golang-lru/v2.New"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22

require github.com/hashicorp/golang-lru/v2 v2.0.7
go.sum
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
main.go
package main

import (
	"fmt"

	lru "github.com/hashicorp/golang-lru/v2"
)

func main() {
	// Initialize an LRU cache with capacity 2
	cache, err := lru.New[string, int](2)
	if err != nil {
		panic(err)
	}

	// Add entries
	cache.Add("alpha", 1)
	cache.Add("beta", 2)
	fmt.Printf("Cache size: %d\n", cache.Len())

	// Access "alpha" to mark it as most recently used
	if val, ok := cache.Get("alpha"); ok {
		fmt.Printf("alpha: %d\n", val)
	}

	// Add third entry, which will evict "beta" (the least recently used)
	cache.Add("gamma", 3)

	// Verify "beta" was evicted and "alpha" remains
	_, hasBeta := cache.Get("beta")
	_, hasAlpha := cache.Get("alpha")
	fmt.Printf("contains beta: %v, contains alpha: %v\n", hasBeta, hasAlpha)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/hashicorp/golang-lru/v2.New in pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/hashicorp/golang-lru/v2@v2.0.7"
  ],
  "symbols": [
    "github.com/hashicorp/golang-lru/v2.New"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	lru "github.com/hashicorp/golang-lru/v2"
)

func main() {
	// Assertion 1: lru.New returns a non-nil cache and nil error when initialized with a positive capacity
	{
		c, err := lru.New[string, int](3)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected nil error, got %v\n", err)
			os.Exit(1)
		}
		if c == nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected non-nil cache\n")
			os.Exit(1)
		}
		if c.Len() != 0 {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: initial length should be 0, got %d\n", c.Len())
			os.Exit(1)
		}
	}

	// Assertion 2: lru.New returns an error when initialized with zero or negative capacity
	{
		_, errZero := lru.New[string, int](0)
		if errZero == nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected error for size 0\n")
			os.Exit(1)
		}

		_, errNeg := lru.New[string, int](-5)
		if errNeg == nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected error for negative size\n")
			os.Exit(1)
		}
	}

	// Assertion 3: Add inserts new key-value pairs, updates existing keys without increasing length, and reports whether an eviction occurred
	{
		c, err := lru.New[string, int](2)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: setup error: %v\n", err)
			os.Exit(1)
		}

		evicted := c.Add("k1", 100)
		if evicted || c.Len() != 1 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: adding first item should not evict, len should be 1\n")
			os.Exit(1)
		}

		evicted = c.Add("k2", 200)
		if evicted || c.Len() != 2 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: adding second item should not evict, len should be 2\n")
			os.Exit(1)
		}

		evicted = c.Add("k1", 150)
		if evicted || c.Len() != 2 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: updating existing key should not evict or change len\n")
			os.Exit(1)
		}

		val, ok := c.Get("k1")
		if !ok || val != 150 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected updated value 150, got %d (ok=%v)\n", val, ok)
			os.Exit(1)
		}
	}

	// Assertion 4: Get retrieves stored values for present keys and updates recent-ness so that least recently used entries are evicted first upon exceeding capacity
	{
		c, err := lru.New[string, string](2)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: setup error: %v\n", err)
			os.Exit(1)
		}

		c.Add("x", "valX")
		c.Add("y", "valY")

		// Access "x" so "y" becomes least recently used
		valX, okX := c.Get("x")
		if !okX || valX != "valX" {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: Get(x) failed\n")
			os.Exit(1)
		}

		// Adding "z" must evict "y"
		evicted := c.Add("z", "valZ")
		if !evicted {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected eviction when exceeding capacity\n")
			os.Exit(1)
		}

		if _, ok := c.Get("y"); ok {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: y should have been evicted\n")
			os.Exit(1)
		}

		if _, ok := c.Get("x"); !ok {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: x should still be in cache\n")
			os.Exit(1)
		}

		if _, ok := c.Get("z"); !ok {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: z should still be in cache\n")
			os.Exit(1)
		}
	}

	// Assertion 5: Contains and Peek inspect presence and values without altering LRU order, and Purge removes all entries from the cache
	{
		c, err := lru.New[string, int](2)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: setup error: %v\n", err)
			os.Exit(1)
		}

		c.Add("first", 1)
		c.Add("second", 2)

		// Peek and Contains should not alter LRU order ("first" remains oldest)
		val, ok := c.Peek("first")
		if !ok || val != 1 {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: Peek(first) failed\n")
			os.Exit(1)
		}

		if !c.Contains("first") || !c.Contains("second") {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: Contains check failed\n")
			os.Exit(1)
		}

		// Adding "third" must evict "first" because Peek/Contains did not update its recency
		evicted := c.Add("third", 3)
		if !evicted {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: expected eviction\n")
			os.Exit(1)
		}

		if c.Contains("first") {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: first should have been evicted\n")
			os.Exit(1)
		}

		// Purge completely empties the cache
		c.Purge()
		if c.Len() != 0 || c.Contains("second") || c.Contains("third") {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: cache should be empty after Purge\n")
			os.Exit(1)
		}
	}

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

原始种子者

匿名