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

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。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-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")
}

オリジンシーダー

匿名