CodeSampleX

サンプル

github.com/klauspost/compress v1.20.0: s2

検証済みサンプル — golang github.com/klauspost/compress v1.20.0: s2. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました: s2.Encode and s2.Decode roundtrip…

sha256:7c220bdf78087542c3670af3d71292959485833c4b324d6fe04b47530fc7b7a7

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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-02

ケース

HOW
ゴール
verify github.com/klauspost/compress/s2 in pkg:golang/github.com/klauspost/compress@v1.20.0
パッケージ
シンボル
  • github.com/klauspost/compress/s2
作成日
2026-09-02T23:55:09Z

コントラクト

  1. s2.Encode and s2.Decode roundtrip byte slice in block format
  2. s2.NewWriter and s2.NewReader stream compress and decompress data to restore original payload
  3. s2.NewWriter with WriterBetterCompression and WriterBestCompression options compresses and roundtrips
  4. s2.NewWriter and s2.NewReader correctly handle empty input payloads
  5. s2.Decode and s2.NewReader return ErrCorrupt or error when given corrupted or truncated data
  6. s2.Writer and s2.Reader support stream reuse via Reset

ファイル

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

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

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:bb83942a40596b4f74ed6a7366996923e98804a65dd9c80c2a490fe23e95db1e","contract":["s2.Encode and s2.Decode roundtrip byte slice in block format","s2.NewWriter and s2.NewReader stream compress and decompress data to restore original payload","s2.NewWriter with WriterBetterCompression and WriterBestCompression options compresses and roundtrips","s2.NewWriter and s2.NewReader correctly handle empty input payloads","s2.Decode and s2.NewReader return ErrCorrupt or error when given corrupted or truncated data","s2.Writer and s2.Reader support stream reuse via Reset"],"goal":"verify github.com/klauspost/compress/s2 in pkg:golang/github.com/klauspost/compress@v1.20.0","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.20.0"],"schemaVersion":1,"symbols":["github.com/klauspost/compress/s2"]},"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.20.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.20.0","symbols":["github.com/klauspost/compress/s2"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/klauspost/compress v1.20.0
go.sum
github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA=
github.com/klauspost/compress v1.20.0/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
main.go
package main

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

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

func main() {
	original := []byte("Hello, CodeSampleX with klauspost/compress s2!")

	// Block format example
	encoded := s2.Encode(nil, original)
	decoded, err := s2.Decode(nil, encoded)
	if err != nil {
		log.Fatalf("failed to decode s2 block: %v", err)
	}

	// Stream format example
	var buf bytes.Buffer
	writer := s2.NewWriter(&buf)
	if _, err := writer.Write(original); err != nil {
		log.Fatalf("failed to write compressed stream: %v", err)
	}
	if err := writer.Close(); err != nil {
		log.Fatalf("failed to close s2 writer: %v", err)
	}

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

	fmt.Printf("Original:        %s\n", string(original))
	fmt.Printf("Block Decoded:   %s\n", string(decoded))
	fmt.Printf("Stream Decoded:  %s\n", string(decompressed))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/klauspost/compress/s2 in pkg:golang/github.com/klauspost/compress@v1.20.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.20.0"
  ],
  "symbols": [
    "github.com/klauspost/compress/s2"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"

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

func main() {
	// 1. s2.Encode and s2.Decode roundtrip byte slice in block format
	{
		original := []byte("CodeSampleX s2 block format verification payload.")
		encoded := s2.Encode(nil, original)
		if len(encoded) == 0 {
			fmt.Fprintf(os.Stderr, "s2.Encode produced empty output\n")
			os.Exit(1)
		}

		decodedLen, err := s2.DecodedLen(encoded)
		if err != nil {
			fmt.Fprintf(os.Stderr, "s2.DecodedLen failed: %v\n", err)
			os.Exit(1)
		}
		if decodedLen != len(original) {
			fmt.Fprintf(os.Stderr, "s2.DecodedLen got %d, want %d\n", decodedLen, len(original))
			os.Exit(1)
		}

		decoded, err := s2.Decode(nil, encoded)
		if err != nil {
			fmt.Fprintf(os.Stderr, "s2.Decode failed: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(original, decoded) {
			fmt.Fprintf(os.Stderr, "s2.Decode mismatch: got %q, want %q\n", string(decoded), string(original))
			os.Exit(1)
		}
	}

	// 2. s2.NewWriter and s2.NewReader stream compress and decompress data to restore original payload
	{
		original := []byte("CodeSampleX s2 streaming format verification payload with high repetition. " +
			"Repeating stream data ensures blocks and framing are verified. ")
		original = bytes.Repeat(original, 10)

		var buf bytes.Buffer
		zw := s2.NewWriter(&buf)
		if _, err := zw.Write(original); err != nil {
			fmt.Fprintf(os.Stderr, "s2.NewWriter Write failed: %v\n", err)
			os.Exit(1)
		}
		if err := zw.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "s2.NewWriter Close failed: %v\n", err)
			os.Exit(1)
		}

		zr := s2.NewReader(&buf)
		decompressed, err := io.ReadAll(zr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "s2.NewReader ReadAll failed: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(original, decompressed) {
			fmt.Fprintf(os.Stderr, "streaming roundtrip mismatch\n")
			os.Exit(1)
		}
	}

	// 3. s2.NewWriter with WriterBetterCompression and WriterBestCompression options compresses and roundtrips
	{
		sampleData := bytes.Repeat([]byte("High repetition pattern for testing s2 writer compression levels. "), 40)
		for name, opt := range map[string]s2.WriterOption{
			"Better": s2.WriterBetterCompression(),
			"Best":   s2.WriterBestCompression(),
		} {
			var b bytes.Buffer
			zw := s2.NewWriter(&b, opt, s2.WriterBlockSize(64*1024))
			if _, err := zw.Write(sampleData); err != nil {
				fmt.Fprintf(os.Stderr, "s2 writer option %s Write failed: %v\n", name, err)
				os.Exit(1)
			}
			if err := zw.Close(); err != nil {
				fmt.Fprintf(os.Stderr, "s2 writer option %s Close failed: %v\n", name, err)
				os.Exit(1)
			}

			zr := s2.NewReader(&b)
			decomp, err := io.ReadAll(zr)
			if err != nil {
				fmt.Fprintf(os.Stderr, "s2 reader ReadAll for %s failed: %v\n", name, err)
				os.Exit(1)
			}
			if !bytes.Equal(sampleData, decomp) {
				fmt.Fprintf(os.Stderr, "s2 option %s roundtrip mismatch\n", name)
				os.Exit(1)
			}
		}
	}

	// 4. s2.NewWriter and s2.NewReader correctly handle empty input payloads
	{
		var emptyBuf bytes.Buffer
		zwEmpty := s2.NewWriter(&emptyBuf)
		if err := zwEmpty.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "s2 empty writer close failed: %v\n", err)
			os.Exit(1)
		}

		zrEmpty := s2.NewReader(&emptyBuf)
		emptyDecomp, err := io.ReadAll(zrEmpty)
		if err != nil {
			fmt.Fprintf(os.Stderr, "s2 empty reader ReadAll failed: %v\n", err)
			os.Exit(1)
		}
		if len(emptyDecomp) != 0 {
			fmt.Fprintf(os.Stderr, "expected empty decompressed payload, got %d bytes\n", len(emptyDecomp))
			os.Exit(1)
		}
	}

	// 5. s2.Decode and s2.NewReader return ErrCorrupt or error when given corrupted or truncated data
	{
		corruptedBlock := []byte{0xff, 0xff, 0xff, 0xff}
		_, err := s2.Decode(nil, corruptedBlock)
		if err == nil {
			fmt.Fprintf(os.Stderr, "expected error decoding corrupted block, got nil\n")
			os.Exit(1)
		}
		if !errors.Is(err, s2.ErrCorrupt) {
			fmt.Fprintf(os.Stderr, "expected ErrCorrupt, got: %v\n", err)
			os.Exit(1)
		}

		corruptedStream := []byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}
		zrBad := s2.NewReader(bytes.NewReader(corruptedStream))
		_, streamErr := io.ReadAll(zrBad)
		if streamErr == nil {
			fmt.Fprintf(os.Stderr, "expected error reading corrupted stream, got nil\n")
			os.Exit(1)
		}
	}

	// 6. s2.Writer and s2.Reader support stream reuse via Reset
	{
		var buf1 bytes.Buffer
		zwReuse := s2.NewWriter(&buf1)
		p1 := []byte("first stream payload for reusable s2 writer")
		if _, err := zwReuse.Write(p1); err != nil {
			fmt.Fprintf(os.Stderr, "p1 write failed: %v\n", err)
			os.Exit(1)
		}
		if err := zwReuse.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "p1 close failed: %v\n", err)
			os.Exit(1)
		}

		var buf2 bytes.Buffer
		zwReuse.Reset(&buf2)
		p2 := []byte("second stream payload after s2 writer reset")
		if _, err := zwReuse.Write(p2); err != nil {
			fmt.Fprintf(os.Stderr, "p2 write failed: %v\n", err)
			os.Exit(1)
		}
		if err := zwReuse.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "p2 close failed: %v\n", err)
			os.Exit(1)
		}

		zrReuse := s2.NewReader(&buf1)
		res1, err := io.ReadAll(zrReuse)
		if err != nil || !bytes.Equal(res1, p1) {
			fmt.Fprintf(os.Stderr, "p1 mismatch: %v\n", err)
			os.Exit(1)
		}

		zrReuse.Reset(&buf2)
		res2, err := io.ReadAll(zrReuse)
		if err != nil || !bytes.Equal(res2, p2) {
			fmt.Fprintf(os.Stderr, "p2 mismatch: %v\n", err)
			os.Exit(1)
		}
	}

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

オリジンシーダー

匿名