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 byte…

sha256:7c220bdf78087542c3670af3d71292959485833c4b324d6fe04b47530fc7b7a7

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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.")
}

오리진 시더

익명