set client_min_messages to warning;
\set ECHO
\set QUIET 1
-- Turn off echo and keep things quiet.

-- Format the output for nice TAP.
\pset format unaligned
\pset tuples_only true
\pset pager

-- Revert all changes on failure.
\set ON_ERROR_ROLLBACK 1
\set ON_ERROR_STOP true

begin;

select plan(33);

select lives_ok('create temp table uuidtest (id uuid primary key)', 'create temp table');
select lives_ok('insert into uuidtest select uuid_v4()', 'generate v4 uuid');
select is(uuid_recent(), (select * from uuidtest), 'uuid_recent works');
select lives_ok('select uuid_v4()', 'generate v4 uuid');
select isnt(uuid_recent(), (select * from uuidtest), 'uuid_recent still works');

--Generation Functions:

select lives_ok('select uuid_v1()', 'generate v1 uuid');
select lives_ok('select uuid_v1mc()', 'generate v1 random mac uuid');
select lives_ok(E'select uuid_v3(uuid_recent(), \'llll\'::text)', 'generate v3 uuid');
select lives_ok('select uuid_v4()', 'generate v4 uuid');
select lives_ok(E'select uuid_v5(uuid_recent(), \'llll\'::text)', 'generate v5 uuid');
select lives_ok(E'select uuid_url(\'http://pd.if.org\'::text)', 'generate url uuid');
select lives_ok(E'select uuid_dns(\'pd.if.org\'::text)', 'generate url uuid');
select lives_ok(E'select uuid_oid(\'2.25\'::text)', 'generate url uuid');

--uuid_x500(text)
--Returns a version 5 uuid using the DNS namespace.

-- lookups
select is(uuid_nil(), '00000000-0000-0000-0000-000000000000'::uuid, 'get nil uuid');
select is(uuid_ns_dns(), '6ba7b810-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get dns uuid');
select is(uuid_ns_url(), '6ba7b811-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get url uuid');
select is(uuid_ns_oid(), '6ba7b812-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get oid uuid');
select is(uuid_ns_x500(), '6ba7b813-9dad-11d1-80b4-00c04fd430c8'::uuid, 'get x500 uuid');

-- casts
select has_cast('uuid','numeric');
select has_cast('uuid','bytea');
select has_cast('uuid','bit');
select has_cast('uuid','bit varying');
select has_cast('numeric', 'uuid');
select has_cast('bytea', 'uuid');
select has_cast('bit','uuid');
select has_cast('bit varying','uuid');
select is(uuid_ns_dns()::bytea, '\x6ba7b8109dad11d180b400c04fd430c8'::bytea, 'bytea cast');
select is('\x6ba7b8109dad11d180b400c04fd430c8'::bytea::uuid, uuid_ns_dns(), 'bytea uncast');

select is(uuid_recent()::numeric::uuid, uuid_recent(), 'reversible numeric cast');
select is(uuid_recent()::bytea::uuid, uuid_recent(), 'reversible bytea cast');
select is(uuid_recent()::bit(128)::uuid, uuid_recent(), 'reversible bit(128) cast');
select is(uuid_recent()::bit varying::uuid, uuid_recent(), 'reversible bit varying cast');

-- create a whole bunch of timestamp uuids
select lives_ok('insert into uuidtest select uuid_v1() from generate_series(1,10000)', '10000 unique');

select * from finish();

rollback;
\q

functions in purepguuid.sql.

UUID Lookup Functions:

uuid_recent()
returns the most recent uuid generated by this backend.  Returns
the nil uuid if no uuids have been generated yet.

Numeric: treats the UUID as a 128 bit number and converts it to a numeric.

Bit(128) and bit varying: treats the uuid as a 128 bit vector.
Casts to bit(n) where n is shorter than 128 will truncate
the uuid.  Where n is longer than 128, the bit vector
will be zero extended.

Field extraction functions:

uuid_version(uuid)
Returns an integer corresponding to the 4 bit version number.

uuid_macaddr(uuid)
Returns a macaddr type corresponding to the mac address part of the
uuid.  This function can be called on any uuid, not just version 1 uuids.

uuid_timestamp(uuid)
Returns a timestamp corresponding to the timestamp field in the uuid.  You
probably want to use uuid_timestamptz().

uuid_timestamptz(uuid)
Returns a timestamp with time zonecorresponding to the timestamp field in the
uuid.
