CodeSampleX

샘플

github.com/shopspring/decimal v1.2.0

검증된 샘플 — golang github.com/shopspring/decimal v1.2.0. go 1.26 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: decimal.NewFromString parses exact decimal…

sha256:98b0dda6842a7f6abb3056cfe3d42d04422934afcd892938cededc2a6b0c52d1

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

실행 증거

선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.

증거 기준
서명된 컨트랙트 통과
검증 영수증
1
빌드한 서명 키
1
선언된 환경 linux 24 · ubuntu · glibc 2.39 x64 go

검증 실행 환경

환경 컨트랙트 단계 실행일
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

케이스

HOW
목표
verify pkg:golang/github.com/shopspring/decimal@v1.2.0
패키지
생성일
2026-08-28T09:15:43Z

컨트랙트

  1. decimal.NewFromString parses exact decimal representations without binary floating-point inaccuracy
  2. decimal.Decimal arithmetic operations Add, Sub, Mul, and DivRound perform exact arithmetic
  3. decimal.Decimal rounding methods Round and RoundBank round to specified precision using standard and banker's rounding
  4. decimal.Decimal comparison methods Equal, GreaterThan, and LessThan compare numeric values independently of scale representation
  5. decimal.Decimal MarshalJSON and UnmarshalJSON serialize and deserialize decimal values as valid JSON numbers

파일

  • 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 pkg:golang/github.com/shopspring/decimal@v1.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/shopspring/decimal@v1.2.0

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:3b3b5693b834c41119956024afd1b504ffaa00b081d1f3ddbb383ccbbd2aac3b","contract":["decimal.NewFromString parses exact decimal representations without binary floating-point inaccuracy","decimal.Decimal arithmetic operations Add, Sub, Mul, and DivRound perform exact arithmetic","decimal.Decimal rounding methods Round and RoundBank round to specified precision using standard and banker's rounding","decimal.Decimal comparison methods Equal, GreaterThan, and LessThan compare numeric values independently of scale representation","decimal.Decimal MarshalJSON and UnmarshalJSON serialize and deserialize decimal values as valid JSON numbers"],"goal":"verify pkg:golang/github.com/shopspring/decimal@v1.2.0","kind":"HOW","packages":["pkg:golang/github.com/shopspring/decimal@v1.2.0"],"schemaVersion":1},"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/shopspring/decimal@v1.2.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/shopspring/decimal@v1.2.0","verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/shopspring/decimal v1.2.0
go.sum
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
main.go
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/shopspring/decimal"
)

// FinancialCalculator demonstrates precision arithmetic and rounding with decimal.Decimal.
type FinancialCalculator struct {
	baseAmount decimal.Decimal
}

// NewFinancialCalculator creates a calculator with a base decimal amount.
func NewFinancialCalculator(amountStr string) (FinancialCalculator, error) {
	d, err := decimal.NewFromString(amountStr)
	if err != nil {
		return FinancialCalculator{}, err
	}
	return FinancialCalculator{baseAmount: d}, nil
}

// AddTax calculates the total after adding a percentage tax.
func (fc FinancialCalculator) AddTax(rateStr string) (decimal.Decimal, error) {
	rate, err := decimal.NewFromString(rateStr)
	if err != nil {
		return decimal.Zero, err
	}
	tax := fc.baseAmount.Mul(rate)
	return fc.baseAmount.Add(tax), nil
}

// SplitEvenly divides the base amount into n parts rounded to 2 decimal places.
func (fc FinancialCalculator) SplitEvenly(parts int64) decimal.Decimal {
	divisor := decimal.NewFromInt(parts)
	return fc.baseAmount.DivRound(divisor, 2)
}

func main() {
	calc, err := NewFinancialCalculator("100.50")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to initialize calculator: %v\n", err)
		os.Exit(1)
	}

	totalWithTax, err := calc.AddTax("0.0825")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to calculate tax: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Total with tax: %s\n", totalWithTax.StringFixed(2))

	share := calc.SplitEvenly(3)
	fmt.Printf("Split into 3: %s\n", share.StringFixed(2))

	data, err := json.Marshal(map[string]decimal.Decimal{"total": totalWithTax})
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal json: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("JSON: %s\n", string(data))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/shopspring/decimal@v1.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/shopspring/decimal@v1.2.0"
  ]
}
test/contract.go
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/shopspring/decimal"
)

func main() {
	// Assertion 1: decimal.NewFromString parses exact decimal representations without binary floating-point inaccuracy
	d1, err := decimal.NewFromString("0.1")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: decimal.NewFromString failed: %v\n", err)
		os.Exit(1)
	}
	d2, err := decimal.NewFromString("0.2")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: decimal.NewFromString failed: %v\n", err)
		os.Exit(1)
	}
	sum := d1.Add(d2)
	expectedSum, _ := decimal.NewFromString("0.3")
	if !sum.Equal(expectedSum) {
		fmt.Fprintf(os.Stderr, "FAIL: 0.1 + 0.2 != 0.3, got: %s\n", sum.String())
		os.Exit(1)
	}

	// Assertion 2: decimal.Decimal arithmetic operations Add, Sub, Mul, and DivRound perform exact arithmetic
	price, _ := decimal.NewFromString("19.99")
	quantity, _ := decimal.NewFromString("3")
	subtotal := price.Mul(quantity)
	discount, _ := decimal.NewFromString("5.00")
	total := subtotal.Sub(discount)
	if total.String() != "54.97" {
		fmt.Fprintf(os.Stderr, "FAIL: arithmetic mismatch: got %s, expected 54.97\n", total.String())
		os.Exit(1)
	}
	split := total.DivRound(decimal.NewFromInt(4), 2)
	if split.StringFixed(2) != "13.74" {
		fmt.Fprintf(os.Stderr, "FAIL: DivRound mismatch: got %s, expected 13.74\n", split.StringFixed(2))
		os.Exit(1)
	}

	// Assertion 3: decimal.Decimal rounding methods Round and RoundBank round to specified precision using standard and banker's rounding
	valHalfUp, _ := decimal.NewFromString("2.345")
	if rounded := valHalfUp.Round(2).String(); rounded != "2.35" {
		fmt.Fprintf(os.Stderr, "FAIL: Round mismatch: got %s, expected 2.35\n", rounded)
		os.Exit(1)
	}
	valBank1, _ := decimal.NewFromString("2.345")
	if roundBank := valBank1.RoundBank(2).String(); roundBank != "2.34" {
		fmt.Fprintf(os.Stderr, "FAIL: RoundBank(2.345) mismatch: got %s, expected 2.34\n", roundBank)
		os.Exit(1)
	}
	valBank2, _ := decimal.NewFromString("2.355")
	if roundBank := valBank2.RoundBank(2).String(); roundBank != "2.36" {
		fmt.Fprintf(os.Stderr, "FAIL: RoundBank(2.355) mismatch: got %s, expected 2.36\n", roundBank)
		os.Exit(1)
	}

	// Assertion 4: decimal.Decimal comparison methods Equal, GreaterThan, and LessThan compare numeric values independently of scale representation
	a, _ := decimal.NewFromString("100.500")
	b, _ := decimal.NewFromString("100.5")
	c, _ := decimal.NewFromString("100.51")
	if !a.Equal(b) {
		fmt.Fprintf(os.Stderr, "FAIL: Equal failed for trailing zeros: %s vs %s\n", a.String(), b.String())
		os.Exit(1)
	}
	if !c.GreaterThan(a) || !a.LessThan(c) {
		fmt.Fprintf(os.Stderr, "FAIL: comparison failed: a=%s, c=%s\n", a.String(), c.String())
		os.Exit(1)
	}

	// Assertion 5: decimal.Decimal MarshalJSON and UnmarshalJSON serialize and deserialize decimal values as valid JSON numbers
	type Item struct {
		Price decimal.Decimal `json:"price"`
	}
	item := Item{Price: price}
	jsonBytes, err := json.Marshal(item)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalJSON failed: %v\n", err)
		os.Exit(1)
	}
	var unmarshaled Item
	if err := json.Unmarshal(jsonBytes, &unmarshaled); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalJSON failed: %v\n", err)
		os.Exit(1)
	}
	if !unmarshaled.Price.Equal(price) {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalJSON value mismatch: got %s, expected %s\n", unmarshaled.Price.String(), price.String())
		os.Exit(1)
	}

	fmt.Println("PASS: pkg:golang/github.com/shopspring/decimal@v1.2.0 contract verified")
}

오리진 시더

익명