CodeSampleX

샘플

github.com/shopspring/decimal v1.2.0: Decimal

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

sha256:3d2ca656ec0a9149d40b6f83c56ac358f140116fe10c722726c2a965a96bf4e1

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

케이스

HOW
목표
verify github.com/shopspring/decimal.Decimal in pkg:golang/github.com/shopspring/decimal@v1.2.0
패키지
심벌
  • github.com/shopspring/decimal.Decimal
생성일
2026-09-04T01:14:15Z

컨트랙트

  1. decimal.Decimal constructors NewFromInt, NewFromString, and RequireFromString initialize exact fixed-point numbers
  2. decimal.Decimal arithmetic methods Add, Sub, Mul, Mod, and Abs perform exact decimal calculations
  3. decimal.Decimal comparison methods Cmp, Equal, Sign, IsZero, and IsNegative correctly evaluate numerical relations
  4. decimal.Decimal methods Truncate, Floor, Ceil, and Shift adjust precision and decimal places predictably
  5. decimal.Decimal driver.Valuer and sql.Scanner implementations support database serialization and deserialization
  6. decimal.Decimal implements TextMarshaler, TextUnmarshaler, and JSON serialization without floating-point distortion

파일

  • 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/shopspring/decimal.Decimal in 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
Demonstrate these symbols/APIs:
  - github.com/shopspring/decimal.Decimal

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:48c7d0940c0ba7e7535263963cc25c6719d59854f62ce8aee069e832163dc5a7","contract":["decimal.Decimal constructors NewFromInt, NewFromString, and RequireFromString initialize exact fixed-point numbers","decimal.Decimal arithmetic methods Add, Sub, Mul, Mod, and Abs perform exact decimal calculations","decimal.Decimal comparison methods Cmp, Equal, Sign, IsZero, and IsNegative correctly evaluate numerical relations","decimal.Decimal methods Truncate, Floor, Ceil, and Shift adjust precision and decimal places predictably","decimal.Decimal driver.Valuer and sql.Scanner implementations support database serialization and deserialization","decimal.Decimal implements TextMarshaler, TextUnmarshaler, and JSON serialization without floating-point distortion"],"goal":"verify github.com/shopspring/decimal.Decimal in pkg:golang/github.com/shopspring/decimal@v1.2.0","kind":"HOW","packages":["pkg:golang/github.com/shopspring/decimal@v1.2.0"],"schemaVersion":1,"symbols":["github.com/shopspring/decimal.Decimal"]},"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","symbols":["github.com/shopspring/decimal.Decimal"],"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 (
	"database/sql/driver"
	"encoding/json"
	"fmt"
	"os"

	"github.com/shopspring/decimal"
)

// Transaction represents a financial record using fixed-point decimals.
type Transaction struct {
	ID      string          `json:"id"`
	Amount  decimal.Decimal `json:"amount"`
	FeeRate decimal.Decimal `json:"fee_rate"`
}

// Ledger holds an account balance with exact decimal arithmetic.
type Ledger struct {
	balance decimal.Decimal
}

// NewLedger constructs a ledger with an initial balance.
func NewLedger(initial decimal.Decimal) *Ledger {
	return &Ledger{balance: initial}
}

// Balance returns the current balance.
func (l *Ledger) Balance() decimal.Decimal {
	return l.balance
}

// ApplyTransaction calculates fees, deducts them, and updates the balance.
func (l *Ledger) ApplyTransaction(tx Transaction) (decimal.Decimal, error) {
	fee := tx.Amount.Mul(tx.FeeRate).Truncate(2)
	netAmount := tx.Amount.Sub(fee)
	l.balance = l.balance.Add(netAmount)
	return netAmount, nil
}

// Value implements driver.Valuer for database persistence.
func (l *Ledger) Value() (driver.Value, error) {
	return l.balance.Value()
}

func main() {
	startingBalance, err := decimal.NewFromString("1000.00")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to parse starting balance: %v\n", err)
		os.Exit(1)
	}

	ledger := NewLedger(startingBalance)

	tx := Transaction{
		ID:      "tx-001",
		Amount:  decimal.RequireFromString("250.75"),
		FeeRate: decimal.NewFromFloat(0.015),
	}

	net, err := ledger.ApplyTransaction(tx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to apply transaction: %v\n", err)
		os.Exit(1)
	}

	data, err := json.Marshal(tx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal transaction: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Transaction JSON: %s\n", string(data))
	fmt.Printf("Net added: %s\n", net.StringFixed(2))
	fmt.Printf("Current balance: %s\n", ledger.Balance().StringFixed(2))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/shopspring/decimal.Decimal in pkg:golang/github.com/shopspring/decimal@v1.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/shopspring/decimal@v1.2.0"
  ],
  "symbols": [
    "github.com/shopspring/decimal.Decimal"
  ]
}
test/contract.go
package main

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

	"github.com/shopspring/decimal"
)

func main() {
	// Assertion 1: decimal.Decimal constructors NewFromInt, NewFromString, and RequireFromString initialize exact fixed-point numbers
	intVal := decimal.NewFromInt(125)
	if intVal.String() != "125" {
		fmt.Fprintf(os.Stderr, "FAIL: NewFromInt(125) got %s, expected 125\n", intVal.String())
		os.Exit(1)
	}

	strVal, err := decimal.NewFromString("123.456")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: NewFromString failed: %v\n", err)
		os.Exit(1)
	}
	if strVal.String() != "123.456" {
		fmt.Fprintf(os.Stderr, "FAIL: NewFromString scale mismatch: %s\n", strVal.String())
		os.Exit(1)
	}

	reqVal := decimal.RequireFromString("123.456")
	if !strVal.Equal(reqVal) {
		fmt.Fprintf(os.Stderr, "FAIL: RequireFromString mismatch: %s vs %s\n", strVal.String(), reqVal.String())
		os.Exit(1)
	}

	// Verify RequireFromString panics on invalid input
	func() {
		defer func() {
			if r := recover(); r == nil {
				fmt.Fprintf(os.Stderr, "FAIL: RequireFromString did not panic on invalid string\n")
				os.Exit(1)
			}
		}()
		_ = decimal.RequireFromString("not-a-number")
	}()

	// Assertion 2: decimal.Decimal arithmetic methods Add, Sub, Mul, Mod, and Abs perform exact decimal calculations
	a := decimal.RequireFromString("10.50")
	b := decimal.RequireFromString("3.25")

	if sum := a.Add(b); !sum.Equal(decimal.RequireFromString("13.75")) {
		fmt.Fprintf(os.Stderr, "FAIL: Add mismatch: %s\n", sum.String())
		os.Exit(1)
	}
	if diff := a.Sub(b); !diff.Equal(decimal.RequireFromString("7.25")) {
		fmt.Fprintf(os.Stderr, "FAIL: Sub mismatch: %s\n", diff.String())
		os.Exit(1)
	}
	if prod := a.Mul(b); !prod.Equal(decimal.RequireFromString("34.125")) {
		fmt.Fprintf(os.Stderr, "FAIL: Mul mismatch: %s\n", prod.String())
		os.Exit(1)
	}
	if rem := a.Mod(b); !rem.Equal(decimal.RequireFromString("0.75")) {
		fmt.Fprintf(os.Stderr, "FAIL: Mod mismatch: %s\n", rem.String())
		os.Exit(1)
	}
	neg := decimal.RequireFromString("-10.50")
	if !neg.Abs().Equal(a) {
		fmt.Fprintf(os.Stderr, "FAIL: Abs mismatch: %s\n", neg.Abs().String())
		os.Exit(1)
	}

	// Assertion 3: decimal.Decimal comparison methods Cmp, Equal, Sign, IsZero, and IsNegative correctly evaluate numerical relations
	c1 := decimal.RequireFromString("100.500")
	c2 := decimal.RequireFromString("100.50")
	c3 := decimal.RequireFromString("200.00")

	if !c1.Equal(c2) || c1.Cmp(c2) != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: Equal / Cmp failed for equivalent values\n")
		os.Exit(1)
	}
	if c1.Cmp(c3) != -1 || c3.Cmp(c1) != 1 {
		fmt.Fprintf(os.Stderr, "FAIL: Cmp ordering mismatch between c1 and c3\n")
		os.Exit(1)
	}
	if !c3.GreaterThan(c1) || !c1.LessThan(c3) {
		fmt.Fprintf(os.Stderr, "FAIL: GreaterThan / LessThan mismatch\n")
		os.Exit(1)
	}

	zeroVal := decimal.Zero
	if zeroVal.Sign() != 0 || !zeroVal.IsZero() || zeroVal.IsNegative() {
		fmt.Fprintf(os.Stderr, "FAIL: Zero valuation failed\n")
		os.Exit(1)
	}
	if neg.Sign() != -1 || !neg.IsNegative() || neg.IsZero() {
		fmt.Fprintf(os.Stderr, "FAIL: Negative valuation failed\n")
		os.Exit(1)
	}
	if a.Sign() != 1 || !a.IsPositive() || a.IsNegative() {
		fmt.Fprintf(os.Stderr, "FAIL: Positive valuation failed\n")
		os.Exit(1)
	}

	// Assertion 4: decimal.Decimal methods Truncate, Floor, Ceil, and Shift adjust precision and decimal places predictably
	precVal := decimal.RequireFromString("12.3456")
	if precVal.Truncate(2).String() != "12.34" {
		fmt.Fprintf(os.Stderr, "FAIL: Truncate(2) mismatch: %s\n", precVal.Truncate(2).String())
		os.Exit(1)
	}
	if precVal.Truncate(0).String() != "12" {
		fmt.Fprintf(os.Stderr, "FAIL: Truncate(0) mismatch: %s\n", precVal.Truncate(0).String())
		os.Exit(1)
	}
	if precVal.Floor().String() != "12" {
		fmt.Fprintf(os.Stderr, "FAIL: Floor mismatch: %s\n", precVal.Floor().String())
		os.Exit(1)
	}
	if decimal.RequireFromString("-12.1").Floor().String() != "-13" {
		fmt.Fprintf(os.Stderr, "FAIL: Negative Floor mismatch\n")
		os.Exit(1)
	}
	if precVal.Ceil().String() != "13" {
		fmt.Fprintf(os.Stderr, "FAIL: Ceil mismatch: %s\n", precVal.Ceil().String())
		os.Exit(1)
	}
	if decimal.RequireFromString("-12.9").Ceil().String() != "-12" {
		fmt.Fprintf(os.Stderr, "FAIL: Negative Ceil mismatch\n")
		os.Exit(1)
	}
	if precVal.Shift(2).String() != "1234.56" {
		fmt.Fprintf(os.Stderr, "FAIL: Shift(2) mismatch: %s\n", precVal.Shift(2).String())
		os.Exit(1)
	}
	if precVal.Shift(-2).String() != "0.123456" {
		fmt.Fprintf(os.Stderr, "FAIL: Shift(-2) mismatch: %s\n", precVal.Shift(-2).String())
		os.Exit(1)
	}

	// Assertion 5: decimal.Decimal driver.Valuer and sql.Scanner implementations support database serialization and deserialization
	sqlVal := decimal.RequireFromString("456.78")
	val, err := sqlVal.Value()
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Value() returned error: %v\n", err)
		os.Exit(1)
	}
	valStr, ok := val.(string)
	if !ok || valStr != "456.78" {
		fmt.Fprintf(os.Stderr, "FAIL: Value() did not return expected string: %v\n", val)
		os.Exit(1)
	}

	var scanned decimal.Decimal
	if err := scanned.Scan(valStr); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Scan(string) returned error: %v\n", err)
		os.Exit(1)
	}
	if !scanned.Equal(sqlVal) {
		fmt.Fprintf(os.Stderr, "FAIL: Scan result mismatch: got %s, expected %s\n", scanned.String(), sqlVal.String())
		os.Exit(1)
	}

	var scannedBytes decimal.Decimal
	if err := scannedBytes.Scan([]byte("456.78")); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Scan([]byte) returned error: %v\n", err)
		os.Exit(1)
	}
	if !scannedBytes.Equal(sqlVal) {
		fmt.Fprintf(os.Stderr, "FAIL: Scan([]byte) result mismatch: got %s\n", scannedBytes.String())
		os.Exit(1)
	}

	// Assertion 6: decimal.Decimal implements TextMarshaler, TextUnmarshaler, and JSON serialization without floating-point distortion
	largeDec := decimal.RequireFromString("9876543210.123456789")
	textBytes, err := largeDec.MarshalText()
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalText failed: %v\n", err)
		os.Exit(1)
	}
	if string(textBytes) != "9876543210.123456789" {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalText content mismatch: %s\n", string(textBytes))
		os.Exit(1)
	}

	var unmarshaledText decimal.Decimal
	if err := unmarshaledText.UnmarshalText(textBytes); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalText failed: %v\n", err)
		os.Exit(1)
	}
	if !unmarshaledText.Equal(largeDec) {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalText value mismatch: %s\n", unmarshaledText.String())
		os.Exit(1)
	}

	type Payload struct {
		Amount decimal.Decimal `json:"amount"`
	}
	jsonBytes, err := json.Marshal(Payload{Amount: largeDec})
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: json.Marshal failed: %v\n", err)
		os.Exit(1)
	}
	var unmarshaledJSON Payload
	if err := json.Unmarshal(jsonBytes, &unmarshaledJSON); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: json.Unmarshal failed: %v\n", err)
		os.Exit(1)
	}
	if !unmarshaledJSON.Amount.Equal(largeDec) {
		fmt.Fprintf(os.Stderr, "FAIL: JSON unmarshaled amount mismatch: %s\n", unmarshaledJSON.Amount.String())
		os.Exit(1)
	}

	fmt.Println("PASS: github.com/shopspring/decimal.Decimal contract verified")
}

오리진 시더

익명