The last post was a simple poem generated by ChatGPT which praised the qualities of Delphi. But ChatGPT is storming the world right now and it’s not because it can generate poems. It’s much more than that. To demonstrate, here is the question posed today to ChatGPT and its response.
Please generate DUnit test cases to adequately cover the TCountdownEvent system class in Delphi.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 unit TestCountdownEvent; interface uses TestFramework, System.SyncObjs; type TestTCountdownEvent = class(TTestCase) private FEvent: TCountdownEvent; public procedure SetUp; override; procedure TearDown; override; published procedure TestInitialCount; procedure TestSignal; procedure TestWaitFor; end; implementation procedure TestTCountdownEvent.SetUp; begin FEvent := TCountdownEvent.Create(3); end; procedure TestTCountdownEvent.TearDown; begin FEvent.Free; end; procedure TestTCountdownEvent.TestInitialCount; begin CheckEquals(3, FEvent.InitialCount); end; procedure TestTCountdownEvent.TestSignal; begin FEvent.Signal; CheckEquals(2, FEvent.CurrentCount); FEvent.Signal(2); CheckEquals(0, FEvent.CurrentCount); end; procedure TestTCountdownEvent.TestWaitFor; var LStartTime: TDateTime; LElapsedTime: TDateTime; begin LStartTime := Now; FEvent.WaitFor(1000); LElapsedTime := Now - LStartTime; CheckTrue(LElapsedTime >= 1.0, 'WaitFor took less than 1 second'); end; initialization RegisterTest(TestTCountdownEvent.Suite); end.
The initial response did not provide adequate test coverage, but it was certainly more complete than I anticipated. A very minor problem was that it used the
Now function but did not include System.SysUtils in the Uses clause. The bigger problem is the WaitFor testcase is simply inaccurate as-generated. I asked it to correct the code, which it did without issue. I then asked it to generate some more tests.
Please add a few more tests. For example, test if you add a zero or a negative initial value using TryAddCount that an exception occurs. Test if you attempt to Signal when the count is zero that an exception occurs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 procedure TestTCountdownEvent.TestTryAddCount; begin CheckFalse(FEvent.TryAddCount(-1), 'TryAddCount with negative count succeeded'); CheckFalse(FEvent.TryAddCount(0), 'TryAddCount with zero count succeeded'); end; procedure TestTCountdownEvent.TestSignalWhenZero; begin FEvent.Signal(3); CheckEquals(0, FEvent.CurrentCount, 'Initial count not reduced to zero'); CheckException(ECountdownEventException, procedure begin FEvent.Signal end, 'Signal did not raise exception when count is zero'); end;
It attempted to do as requested, but the results were incorrect. After a little more back and forth, it eventually produced the requested code.
So, the results are not perfect, but are still somewhat impressive. Will you use ChatGPT to help you code in Delphi? It might be a little too early to rely on code generation for Delphi, but I assume that the results will improve over time and this will be an extremely powerful tool for any developer. Many developers are already including ChatGPT in their daily routines. I have even seen a few different ChatGPT plugins for Delphi being released. ChatGPT is here to stay and it will certainly change the future…hopefully for the better! It does seem to be inherently biased on the political/social side of things but we will leave those discussions to be had over a beer.