Happy tickets benchmark

| category: Testing | author: st
Tags: , , ,

Some countries have integer numbers printed on the transport tickets, for example, in ex-USSR. The ticket is "happy" when the sum of the first half of digits equals to the second one. Then you should eat it and make a wish.

Example:

123456 and 111222 are not happy tickets
123123 …

Sorted map vs hashed map

| category: Testing | author: st
Tags:

In theory, hash map structure should be very fast on retrieving by key operations, close to O(1) like for an array. A sorted map (keys are sorted) should be O(log2 N) instead. The following test checks the difference.

Common scenario

We will use one tester class per …

Watching heap memory usage in Free Pascal/Delphi

| category: Testing | author: st
Tags: ,

Free Pascal (like Delphi) provide some useful functions to watch heap memory state in your application.

function GetHeapStatus: THeapStatus;

THeapStatus is documented here

Example of using this function is below

program ProgWatchMem;

uses
  Math, FGL;

type
  TLongIntArr = array of LongInt; // LongInt is a 4-byte signed integer
  PLongIntArr = ^TLongIntArr;
  TMySortedMap = specialize …

Delphi/Free Pascal: very simple "old school" mutex

| category: Programming | author: st
Tags: ,

The code below show how to implement very simple but cross-platform "old school" mutex (mutually exclusive semaphore)

interface

type
  TMutex = class
  private
    FFileHandle: integer;
  public
    constructor Create(const AName: string; const WaitForMSec: integer = 10000);
    destructor Destroy; override;
  end;

implementation

uses
  Classes, SysUtils, DateUtils,
  {$IFDEF MSWINDOWS}
  Windows
  {$ENDIF};

function GetTempDir: string …