spec/util_uuid_spec.lua
author Kim Alvefur <zash@zash.se>
Sat, 24 Feb 2024 00:05:29 +0100
changeset 13443 1e229d710a3c
parent 13321 e6a5f196fc1f
permissions -rw-r--r--
util.signal: Add support for signalfd(2) on Linux signalfd allows handling signal events using the same method as sockets, via file descriptors. Thus all signal dispatch can go through the same main event loop as everything else, removing need for thread-scary signal handling where execution would just jump to the signal handler regardless of the state of Lua, and needing to keep track of Lua states/threads.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     1
-- This tests the format, not the randomness
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     2
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     3
local uuid = require "util.uuid";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     4
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     5
describe("util.uuid", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     6
	describe("#generate()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     7
		it("should work follow the UUID pattern", function()
12608
bd9e006a7a74 various: Update IETF RFC URLs for tools.ietf.org transition
Kim Alvefur <zash@zash.se>
parents: 8244
diff changeset
     8
			-- https://www.rfc-editor.org/rfc/rfc4122.html#section-4.4
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
     9
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    10
			local pattern = "^" .. table.concat({
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    11
				string.rep("%x", 8),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    12
				string.rep("%x", 4),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    13
				"4" .. -- version
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    14
				string.rep("%x", 3),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    15
				"[89ab]" .. -- reserved bits of 1 and 0
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    16
				string.rep("%x", 3),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    17
				string.rep("%x", 12),
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    18
			}, "%-") .. "$";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    19
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    20
			for _ = 1, 100 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    21
				assert.is_string(uuid.generate():match(pattern));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    22
			end
13321
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    23
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    24
			assert.truthy(uuid.generate() ~= uuid.generate(), "does not generate the same UUIDv4 twice")
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    25
		end);
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    26
	end);
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    27
	describe("#v7", function()
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    28
		it("should also follow the UUID pattern", function()
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    29
			local pattern = "^" .. table.concat({
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    30
					string.rep("%x", 8),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    31
					string.rep("%x", 4),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    32
					"7" .. -- version
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    33
					string.rep("%x", 3),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    34
					"[89ab]" .. -- reserved bits of 1 and 0
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    35
					string.rep("%x", 3),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    36
					string.rep("%x", 12),
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    37
				}, "%-") .. "$";
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    38
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    39
			local one = uuid.v7(); -- one before the loop to ensure some time passes
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    40
			for _ = 1, 100 do
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    41
				assert.is_string(uuid.v7():match(pattern));
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    42
			end
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    43
			-- one after the loop when some time should have passed
e6a5f196fc1f util.uuid: Add UUIDv7
Kim Alvefur <zash@zash.se>
parents: 12608
diff changeset
    44
			assert.truthy(one < uuid.v7(), "should be ordererd")
8239
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    45
		end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    46
	end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
    47
end);