CodeSampleX

示例

github.com/klauspost/compress v1.19.1: zlib.DefaultCompression

已验证示例 — golang github.com/klauspost/compress v1.19.1: zlib.DefaultCompression. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:a1cbfefac5da841a8cbfb74f990e8ef27747aa8025f7fa12129f2091a215c660

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

案例

HOW
目标
verify github.com/klauspost/compress/zlib.DefaultCompression in pkg:golang/github.com/klauspost/compress@v1.19.1
包
符号
  • github.com/klauspost/compress/zlib.DefaultCompression
创建时间
2026-10-11T06:24:44Z

契约

  1. zlib.DefaultCompression constant equals -1
  2. zlib.NewWriterLevel with zlib.DefaultCompression compresses payload and roundtrips to original content via zlib.NewReader
  3. zlib.NewWriterLevel with zlib.DefaultCompression compresses empty input and emits a valid zlib stream
  4. zlib.NewWriterLevel with zlib.DefaultCompression Flush pushes buffered compressed data to underlying writer
  5. zlib.NewWriterLevelDict with zlib.DefaultCompression initializes a writer with custom dictionary and roundtrips correctly
  6. zlib.NewWriterLevel with zlib.DefaultCompression Reset resets the writer destination and allows reuse

文件

  • 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/klauspost/compress/zlib.DefaultCompression in pkg:golang/github.com/klauspost/compress@v1.19.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/compress@v1.19.1
Demonstrate these symbols/APIs:
  - github.com/klauspost/compress/zlib.DefaultCompression

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:96ec299f15edab55dd31a9ac5d19899ff8c5afee4c9223c1bff330358823e3ae","contract":["zlib.DefaultCompression constant equals -1","zlib.NewWriterLevel with zlib.DefaultCompression compresses payload and roundtrips to original content via zlib.NewReader","zlib.NewWriterLevel with zlib.DefaultCompression compresses empty input and emits a valid zlib stream","zlib.NewWriterLevel with zlib.DefaultCompression Flush pushes buffered compressed data to underlying writer","zlib.NewWriterLevelDict with zlib.DefaultCompression initializes a writer with custom dictionary and roundtrips correctly","zlib.NewWriterLevel with zlib.DefaultCompression Reset resets the writer destination and allows reuse"],"goal":"verify github.com/klauspost/compress/zlib.DefaultCompression in pkg:golang/github.com/klauspost/compress@v1.19.1","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.19.1"],"schemaVersion":1,"symbols":["github.com/klauspost/compress/zlib.DefaultCompression"]},"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/klauspost/compress@v1.19.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.19.1","symbols":["github.com/klauspost/compress/zlib.DefaultCompression"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/klauspost/compress v1.19.1
go.sum
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"log"

	"github.com/klauspost/compress/zlib"
)

func main() {
	originalData := []byte("Demonstrating zlib compression using DefaultCompression level.")

	var compressedBuf bytes.Buffer
	writer, err := zlib.NewWriterLevel(&compressedBuf, zlib.DefaultCompression)
	if err != nil {
		log.Fatalf("failed to create zlib writer: %v", err)
	}

	if _, err := writer.Write(originalData); err != nil {
		log.Fatalf("failed to write data: %v", err)
	}

	if err := writer.Close(); err != nil {
		log.Fatalf("failed to close zlib writer: %v", err)
	}

	compressedLen := compressedBuf.Len()

	reader, err := zlib.NewReader(&compressedBuf)
	if err != nil {
		log.Fatalf("failed to create zlib reader: %v", err)
	}
	defer reader.Close()

	decompressed, err := io.ReadAll(reader)
	if err != nil {
		log.Fatalf("failed to read decompressed data: %v", err)
	}

	fmt.Printf("Default compression level: %d\n", zlib.DefaultCompression)
	fmt.Printf("Original size: %d, Compressed size: %d\n", len(originalData), compressedLen)
	fmt.Printf("Decompressed data matches: %t\n", bytes.Equal(originalData, decompressed))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/klauspost/compress/zlib.DefaultCompression in pkg:golang/github.com/klauspost/compress@v1.19.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.19.1"
  ],
  "symbols": [
    "github.com/klauspost/compress/zlib.DefaultCompression"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"compress/flate"
	"fmt"
	"io"
	"os"

	"github.com/klauspost/compress/zlib"
)

func main() {
	// Assertion 1: zlib.DefaultCompression constant equals -1
	if zlib.DefaultCompression != -1 || zlib.DefaultCompression != flate.DefaultCompression {
		fmt.Fprintf(os.Stderr, "assertion 1 failed: expected DefaultCompression to equal -1, got %d\n", zlib.DefaultCompression)
		os.Exit(1)
	}

	// Assertion 2: zlib.NewWriterLevel with zlib.DefaultCompression compresses payload and roundtrips to original content via zlib.NewReader
	{
		payload := bytes.Repeat([]byte("Contract assertion testing DefaultCompression payload compression roundtrip. "), 20)
		var buf bytes.Buffer
		zw, err := zlib.NewWriterLevel(&buf, zlib.DefaultCompression)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: NewWriterLevel error: %v\n", err)
			os.Exit(1)
		}
		if _, err := zw.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: Close error: %v\n", err)
			os.Exit(1)
		}

		zr, err := zlib.NewReader(&buf)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: NewReader error: %v\n", err)
			os.Exit(1)
		}
		decomp, err := io.ReadAll(zr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: ReadAll error: %v\n", err)
			os.Exit(1)
		}
		if err := zr.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: zr.Close error: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(payload, decomp) {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: decompressed payload mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 3: zlib.NewWriterLevel with zlib.DefaultCompression compresses empty input and emits a valid zlib stream
	{
		var buf bytes.Buffer
		zw, err := zlib.NewWriterLevel(&buf, zlib.DefaultCompression)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: NewWriterLevel error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: Close error: %v\n", err)
			os.Exit(1)
		}
		if buf.Len() == 0 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected non-empty zlib header/footer, got 0 bytes\n")
			os.Exit(1)
		}

		zr, err := zlib.NewReader(&buf)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: NewReader error: %v\n", err)
			os.Exit(1)
		}
		decomp, err := io.ReadAll(zr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: ReadAll error: %v\n", err)
			os.Exit(1)
		}
		if err := zr.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: zr.Close error: %v\n", err)
			os.Exit(1)
		}
		if len(decomp) != 0 {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected 0 bytes decompressed, got %d\n", len(decomp))
			os.Exit(1)
		}
	}

	// Assertion 4: zlib.NewWriterLevel with zlib.DefaultCompression Flush pushes buffered compressed data to underlying writer
	{
		var buf bytes.Buffer
		zw, err := zlib.NewWriterLevel(&buf, zlib.DefaultCompression)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: NewWriterLevel error: %v\n", err)
			os.Exit(1)
		}
		msg := []byte("flush verification for zlib writer using default compression level")
		if _, err := zw.Write(msg); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Flush(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: Flush error: %v\n", err)
			os.Exit(1)
		}
		if buf.Len() == 0 {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: expected flushed bytes in buffer, got 0\n")
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: Close error: %v\n", err)
			os.Exit(1)
		}

		zr, err := zlib.NewReader(&buf)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: NewReader error: %v\n", err)
			os.Exit(1)
		}
		decomp, err := io.ReadAll(zr)
		if err != nil || !bytes.Equal(decomp, msg) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: decomp mismatch after Flush\n")
			os.Exit(1)
		}
		_ = zr.Close()
	}

	// Assertion 5: zlib.NewWriterLevelDict with zlib.DefaultCompression initializes a writer with custom dictionary and roundtrips correctly
	{
		dict := []byte("predefined zlib dictionary terms keywords protocol schemas")
		payload := []byte("predefined zlib dictionary terms repeating words protocol schemas")
		var buf bytes.Buffer
		zwDict, err := zlib.NewWriterLevelDict(&buf, zlib.DefaultCompression, dict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: NewWriterLevelDict error: %v\n", err)
			os.Exit(1)
		}
		if _, err := zwDict.Write(payload); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zwDict.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: Close error: %v\n", err)
			os.Exit(1)
		}

		zrDict, err := zlib.NewReaderDict(&buf, dict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: NewReaderDict error: %v\n", err)
			os.Exit(1)
		}
		decomp, err := io.ReadAll(zrDict)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: ReadAll error: %v\n", err)
			os.Exit(1)
		}
		if err := zrDict.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: zrDict.Close error: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(payload, decomp) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: dictionary decompression mismatch\n")
			os.Exit(1)
		}
	}

	// Assertion 6: zlib.NewWriterLevel with zlib.DefaultCompression Reset resets the writer destination and allows reuse
	{
		var buf1, buf2 bytes.Buffer
		zw, err := zlib.NewWriterLevel(&buf1, zlib.DefaultCompression)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: NewWriterLevel error: %v\n", err)
			os.Exit(1)
		}
		msg1 := []byte("first stream content before reset")
		if _, err := zw.Write(msg1); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: msg1 Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: msg1 Close error: %v\n", err)
			os.Exit(1)
		}

		zw.Reset(&buf2)
		msg2 := []byte("second stream content after reset")
		if _, err := zw.Write(msg2); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: msg2 Write error: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: msg2 Close error: %v\n", err)
			os.Exit(1)
		}

		zr1, err := zlib.NewReader(&buf1)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: zr1 init error: %v\n", err)
			os.Exit(1)
		}
		out1, err := io.ReadAll(zr1)
		if err != nil || !bytes.Equal(out1, msg1) {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: out1 mismatch\n")
			os.Exit(1)
		}
		_ = zr1.Close()

		zr2, err := zlib.NewReader(&buf2)
		if err != nil {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: zr2 init error: %v\n", err)
			os.Exit(1)
		}
		out2, err := io.ReadAll(zr2)
		if err != nil || !bytes.Equal(out2, msg2) {
			fmt.Fprintf(os.Stderr, "assertion 6 failed: out2 mismatch\n")
			os.Exit(1)
		}
		_ = zr2.Close()
	}

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

原始种子者

匿名