From 707cc94fe52463870a9c6c8e2e66eaaa389e601d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 24 Feb 2009 15:16:26 +0200 Subject: Start tracking XSD/e with git after version 3.0.0 --- tests/cxx/hashmap/driver.cxx | 98 ++++++++++++++++++++++++++++++++++++++++++++ tests/cxx/hashmap/makefile | 61 +++++++++++++++++++++++++++ tests/cxx/hashmap/output | 9 ++++ 3 files changed, 168 insertions(+) create mode 100644 tests/cxx/hashmap/driver.cxx create mode 100644 tests/cxx/hashmap/makefile create mode 100644 tests/cxx/hashmap/output (limited to 'tests/cxx/hashmap') diff --git a/tests/cxx/hashmap/driver.cxx b/tests/cxx/hashmap/driver.cxx new file mode 100644 index 0000000..99c773f --- /dev/null +++ b/tests/cxx/hashmap/driver.cxx @@ -0,0 +1,98 @@ +// file : tests/cxx/hashmap/driver.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +// Test xsde::cxx::hashmap type. +// +#include +#include + +#include + +#include + +using xsde::cxx::hashmap; +using namespace std; + +const char* table[] = +{ + "apple", "1", + "orange", "2", + "peach", "3", + "apricot", "4", + "mango", "5", + "banana", "6", + "pear", "7", + "grapefruit", "8", + "tangerine", "9", +}; + +const size_t table_size = (sizeof (table) / sizeof (const char*)) / 2; + +int +main () +{ + { + hashmap h (1, sizeof (const char*)); + assert (h.size () == 0 && h.empty () && h.max_bucket_size () == 0); + assert (h.begin () == h.end ()); + } + + { + hashmap h (2, sizeof (const char*)); + + const char* v = "bar"; + h.insert ("foo", &v); + assert (h.size () == 1 && !h.empty () && h.max_bucket_size () == 1); + assert (h.begin () != h.end ()); + const void* p = h.find ("foo"); + assert (p != 0 && *static_cast (p) == v); + } + + { + hashmap h (5, sizeof (const char*)); + + for (size_t i = 0; i < table_size; ++i) + { + const char* v = table[i * 2 + 1]; + h.insert (table[i * 2], &v); + } + + assert (h.size () == table_size); + + for (size_t i = 0; i < table_size; ++i) + { + const char* v = table[i * 2 + 1]; + const void* p = h.find (table[i * 2]); + assert (p != 0 && *static_cast (p) == v); + } + + // Figure out how many elements are in each bucket. + // + size_t bucket_entries[5] = {0, 0, 0, 0, 0}; + + for (size_t i = 0; i < table_size; ++i) + { + bucket_entries[hashmap::hash (table[i * 2]) % 5]++; + } + + size_t max = 0; + + for (size_t i = 0; i < 5; ++i) + { + if (bucket_entries[i] > max) + max = bucket_entries[i]; + } + + assert (max == h.max_bucket_size ()); + + // Test iteration + // + for (hashmap::const_iterator i = h.begin (); i != h.end (); ++i) + { + const void* p = *i; + cout << "'" << *static_cast (p) << "'" << endl; + } + } +} diff --git a/tests/cxx/hashmap/makefile b/tests/cxx/hashmap/makefile new file mode 100644 index 0000000..e9b3aca --- /dev/null +++ b/tests/cxx/hashmap/makefile @@ -0,0 +1,61 @@ +# file : tests/cxx/hashmap/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2006-2009 Code Synthesis Tools CC +# license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../../build/bootstrap.make + +cxx := driver.cxx + +obj := $(addprefix $(out_base)/,$(cxx:.cxx=.o)) +dep := $(obj:.o=.o.d) + +xsde.l := $(out_root)/libxsde/xsde/xsde.l +xsde.l.cpp-options := $(out_root)/libxsde/xsde/xsde.l.cpp-options + +driver := $(out_base)/driver +test := $(out_base)/.test +clean := $(out_base)/.clean + + +# Build. +# +$(driver): $(obj) $(xsde.l) + +$(obj) $(dep): $(xsde.l.cpp-options) + +$(call include-dep,$(dep)) + +# Convenience alias for default target. +# +.PHONY: $(out_base)/ +$(out_base)/: $(driver) + + +# Test. +# +.PHONY: $(test) + +$(test): driver := $(driver) +$(test): $(driver) $(src_base)/output + $(call message,test $$1,$$1 | diff -u $(src_base)/output -,$(driver)) + +# Clean. +# +.PHONY: $(clean) + +$(clean): $(driver).o.clean \ + $(addsuffix .cxx.clean,$(obj)) \ + $(addsuffix .cxx.clean,$(dep)) + + +# How to. +# +$(call include,$(bld_root)/cxx/o-e.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) + + +# Dependencies. +# +$(call import,$(src_root)/libxsde/xsde/makefile) diff --git a/tests/cxx/hashmap/output b/tests/cxx/hashmap/output new file mode 100644 index 0000000..295e6da --- /dev/null +++ b/tests/cxx/hashmap/output @@ -0,0 +1,9 @@ +'1' +'4' +'2' +'9' +'3' +'8' +'5' +'6' +'7' -- cgit v1.1