From 1caa150b5c4007c93be597c473abb7a05f1bd399 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 9 Jul 2014 20:25:28 -0500 Subject: [PATCH] Add a testable example. This commit creates and an example test file that integrates nicely with Go's example tooling. This allows the example output to be tested as a part of running the normal Go tests to help ensure it doesn't get out of date with the code. --- bloom/example_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bloom/example_test.go diff --git a/bloom/example_test.go b/bloom/example_test.go new file mode 100644 index 0000000..71423f7 --- /dev/null +++ b/bloom/example_test.go @@ -0,0 +1,44 @@ +// Copyright (c) 2014 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package bloom_test + +import ( + "fmt" + "math/rand" + "time" + + "github.com/conformal/btcutil/bloom" + "github.com/conformal/btcwire" +) + +// This example creates a new bloom filter, adds a transaction hash to it, and +// shows how to check if the filter matches the transaction. +func ExampleNewFilter() { + rand.Seed(time.Now().UnixNano()) + tweak := rand.Uint32() + + // Create a new bloom filter intended to hold 10 elements with a 0.01% + // false positive rate and does not include any automatic update + // functionality when transactions are matched. + filter := bloom.NewFilter(10, tweak, 0.0001, btcwire.BloomUpdateNone) + + // Create a transaction hash and add it to the filter. This particular + // trasaction is the first transaction in block 310,000 of the main + // bitcoin block chain. + txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45" + txHash, err := btcwire.NewShaHashFromStr(txHashStr) + if err != nil { + fmt.Println(err) + return + } + filter.AddShaHash(txHash) + + // Show that the filter matches. + matches := filter.Matches(txHash.Bytes()) + fmt.Println("Filter Matches?:", matches) + + // Output: + // Filter Matches?: true +}