-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_cpp.cpp
2428 lines (2035 loc) · 67.7 KB
/
test_cpp.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker and Peter Ludemann
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2022-2024, SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* This is used by test_cpp.pl */
/* Most of these predicates are from test.cpp or the documentation.*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This code may be compiled using
swipl-ld -shared -o test_cpp test_cpp.cpp
and subsequently loading using
swipl
?- use_foreign_library(test_cpp).
Next, run example predicates such as below. Scan through this file
to find the predicates provided by this C++ code.
?- hello(world).
Hello world
This code is also used by test_cpp.pl, which has many examples of
how the various predicates can be called from Prolog.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define _CRT_SECURE_NO_WARNINGS 1
#define PROLOG_MODULE "user"
#include <iostream>
#include <sstream>
#include <memory>
#include "SWI-cpp2.h"
#include "SWI-cpp2-atommap.h"
#include "SWI-cpp2-flags.h"
#include <errno.h>
#include <math.h>
#include <cassert>
#include <cstdio> // for MyFileBlob
#include <limits>
#include <string>
#include <map>
#include <vector>
using namespace std;
#ifdef _MSC_VER
#undef min
#undef max
#endif
#ifdef O_DEBUG
#define DEBUG(g) g
#else
#define DEBUG(g) (void)0
#endif
PREDICATE(unwrap, 1)
{ // test that the definition of C_ as a macro hasn't broken anything
PlTerm A1_copy(A1);
PlTerm *A1_ptr = &A1_copy;
return A1.C_ == A1.unwrap() &&
A1.C_ == *PlUnwrapAsPtr(A1_ptr) &&
A1.unwrap() == *PlUnwrapAsPtr(A1_ptr);
}
PREDICATE(hello, 0)
{ PlQuery q("write", PlTermv(PlTerm_atom("hello hello hello")));
PlCheckFail(q.next_solution());
return true;
}
PREDICATE(hello, 2)
{ std::stringstream buffer;
// This will result in an encoding error if A1 isn't Latin-1
buffer << "Hello " << A1.as_string() << endl;
buffer << "Hello " << A1.as_string().c_str() << endl; // Same output as previous line
buffer << "Hello " << A1.as_string(PlEncoding::Latin1) << endl; // Also same, if it's ASCII
buffer << "Hello " << A1.as_string(PlEncoding::UTF8) << endl;
buffer << "Hello " << A1.as_string(PlEncoding::Locale) << endl; // Can vary by locale settings
return A2.unify_string(buffer.str());
}
PREDICATE(hello2, 2)
{ PlAtom atom_a1(A1.as_atom());
std::stringstream buffer;
// The following have the same output as hello/1, if A1 is an atom
buffer << "Hello2 " << atom_a1.as_string() << endl;
buffer << "Hello2 " << A1.as_string().c_str() << endl;
buffer << "Hello2 " << A1.as_string(PlEncoding::Latin1) << endl;
buffer << "Hello2 " << A1.as_string(PlEncoding::UTF8) << endl;
buffer << "Hello2 " << A1.as_string(PlEncoding::Locale) << endl;
return A2.unify_string(buffer.str());
}
PREDICATE(hello3, 2)
{ PlAtom atom_a1(A1.as_atom());
char buf[1024];
// Iostream doesn't work because `<<` doesn't support std::wstring:
// cout << "Hello3 " << atom_a1.wstring() << endl; /* Same output as hello/1 */
// If %s is used, an error will occur if A1 has a non-ascii
// character in it. In addition, a NUL ('\0') in the atom will cause
// the rest of the atom to not be printed.
int len = Ssnprintf(buf, sizeof buf,
"Hello3 %Ws\n", atom_a1.as_wstring().c_str());
if ( len >= 0 )
// TODO: use len when fixed: https://github.com/SWI-Prolog/swipl-devel/issues/1074
return A2.unify_chars(PL_STRING|REP_UTF8, strlen(buf), buf);
return false;
}
PREDICATE(hello4, 1)
{ // The following code is the same as
// A1.unify_term(PlCompound("hello", PlTermv(PlAtom("world"))));
// but is in separate statements to make tracig constructors easier.
auto hello_world = A1;
auto world_atom = PlAtom("world");
auto world_termv = PlTermv(world_atom);
auto hello_world_compound = PlCompound("hello", world_termv);
return hello_world.unify_term(hello_world_compound);
}
// TODO: add tests
PREDICATE(as_string, 2)
{ return A2.unify_string(A1.as_string());
}
// TODO: add tests
PREDICATE(as_wstring, 2)
{ return A2.unify_wstring(A1.as_wstring());
}
PREDICATE(add, 3)
{ // as_long() converts integral floats to integers
return A3.unify_integer(A1.as_long() + A2.as_long());
}
PREDICATE(add_num, 3)
{ auto x = A1, y = A2, result = A3;
// Note that as_float() handles integers
double sum = x.as_float() + y.as_float();
return ( double(long(sum)) == sum ) // Can float be represented as int?
? result.unify_integer(long(sum))
: result.unify_float(sum);
}
PREDICATE(name_arity, 1)
{ PlStream strm(Scurrent_output);
strm.printf("name = %s, arity = %zd\n", A1.name().as_string().c_str(), A1.arity());
return true;
}
PREDICATE(name_arity, 2)
{ std::stringstream buffer;
buffer << "name = " << A1.name().as_string() << ", arity = " << A1.arity() << endl;
return A2.unify_string(buffer.str());
}
PREDICATE(name_arity, 3) /* name_arity(+Term, -Name, -Arity) */
{ PlTerm term(A1);
PlTerm name(A2);
PlTerm arity(A3);
PlCheckFail(name.unify_atom(term.name()));
PlCheckFail(arity.unify_integer(term.arity()));
return true;
}
PREDICATE(name_arity_bool, 3) // like name_arity/3 but doesn't throw
{ PlTerm term(A1), name(A2), arity(A3);
PlAtom name_a(PlAtom::null);
size_t arity_a;
if ( !term.name_arity(&name_a, &arity_a) )
return false;
assert(name_a.not_null());
return name.unify_atom(name_a) && arity.unify_integer(arity_a);
}
PREDICATE(list_modules, 1)
{ std::stringstream buffer;
PlTermv av(1);
PlQuery q("current_module", av);
while( q.next_solution() )
buffer << av[0].as_string() << endl;
q.cut();
return A1.unify_string(buffer.str());
}
// %! average(+Templ, :Goal, -Average) is det.
// % Same as: aggregate(sum(X)/count, Goal, A), Average is A.
PREDICATE(average, 3) /* average(+Templ, :Goal, -Average) */
{ long sum = 0;
long n = 0;
/* Some compilers (e.g., MSVC) require the following code:
PlTermv av(A2);
PlQuery q("call", av);
*/
PlQuery q("call", PlTermv(A2));
while( q.next_solution() )
{ sum += A1.as_long();
n++;
}
q.cut();
return A3.unify_float(double(sum) / double(n));
}
PREDICATE(call_cpp, 2)
{ PlQuery q(A1.as_string(), PlTermv(A2));
PlCheckFail(q.next_solution());
// There's no need for calling q.cut() - it's done implicitly by the
// query's destructor.
return true;
}
PREDICATE(call_cut, 1)
{ PlQuery q(A1.as_string(), PlTermv());
PlCheckFail(q.next_solution());
q.cut(); // This tests that ~PlQuery() behaves correctly if cut() had been called
return true;
}
// TODO: add tests for PlQuery() with PL_Q_EXT_STATUS
PREDICATE(call_cpp, 1)
{ PlCheckFail(PlCall(A1));
return true;
}
PREDICATE(call_cpp_obj, 1)
{ return A1.call();
}
PREDICATE(call_cpp_ex, 2)
{ try
{ PlCheckFail(PlCall(A1, PL_Q_CATCH_EXCEPTION));
} catch ( PlException& ex )
{ bool rc = A2.unify_term(ex.term());
Plx_clear_exception();
return rc;
}
return A2.unify_string("no exception");
}
PREDICATE(atom_to_string, 2)
{ PlAtom a(A1.as_atom());
PlCheckFail(A2.unify_string(a.as_string(PlEncoding::UTF8)));
return true;
}
PREDICATE(term_to_string, 2)
{ PlCheckFail(A2.unify_string(A1.as_string(PlEncoding::UTF8)));
return true;
}
PREDICATE(term, 1)
{ return A1.unify_term(PlCompound("hello", PlTermv(PlAtom("world"))));
}
PREDICATE(term, 2)
{ static PlAtom ATOM_atom("atom");
PlAtom a(A1.as_atom());
if ( a.unwrap() == ATOM_atom.unwrap() )
return A2.unify_atom("hello world"); // or A2.unify_term(PlAtom("hello world"));
if ( A1.as_string() == "string" )
return A2.unify_string("hello world");
if ( A1.as_string() == "code_list" )
return A2.unify_list_codes("hello world"); // TODO: deprecated
if ( A1.as_string() == "char_list" )
return A2.unify_list_chars("hello world"); // TODO: deprecated
if ( A1.as_string() == "term" )
return A2.unify_term(PlCompound("hello(world)"));
throw PlDomainError("type", A1);
}
PREDICATE(call_chars_discard, 1)
{ PlFrame fr;
PlCompound goal(A1.as_string());
bool rval = goal.call();
fr.discard();
return rval;
}
PREDICATE(call_chars, 1)
{ A1.must_be_atom_or_string();
PlCompound goal(A1.as_string());
return goal.call();
}
/* can_unify(A1, A2) :- unifiable(A1, A2, _) */
PREDICATE(can_unify, 2)
{ PlFrame fr;
bool rval = A1.unify_term(A2);
fr.discard(); // or: PL.rewind()
return rval;
}
PREDICATE(can_unify_ffi, 2)
{ fid_t fid = PL_open_foreign_frame();
int rval = PL_unify(A1.unwrap(), A2.unwrap());
PL_discard_foreign_frame(fid);
return rval;
}
/* if_then(A1,A2,A3,A4) :- A1 = A2 -> once(A3) ; once(A4) */
PREDICATE(if_then_a, 4)
{ PlFrame fr;
bool t1_t2_unified = A1.unify_term(A2);
if ( ! t1_t2_unified )
fr.rewind();
return PlCall(t1_t2_unified ? A3 : A4);
}
/* if_then(A1,A2,A3,A4) :- A1 = A2 -> once(A3) ; once(A4) */
PREDICATE(if_then_b, 4)
{ return PlCall(PlRewindOnFail([t1=A1,t2=A2]()->bool
{ return t1.unify_term(t2); }) ? A3 : A4);
}
PREDICATE(if_then_ffi, 4)
{ fid_t fid = PL_open_foreign_frame();
term_t t1 = A1.unwrap(), t2 = A2.unwrap();
int t1_t2_unified = PL_unify(t1, t2);
if ( !t1_t2_unified )
{ if ( PL_exception(0) )
{ PL_close_foreign_frame(fid);
return FALSE;
}
PL_rewind_foreign_frame(fid);
}
int rc;
rc = PL_call((t1_t2_unified ? A3 : A4).unwrap(), (module_t)0);
PL_close_foreign_frame(fid);
return rc;
}
PREDICATE(eq1, 2)
{ PlCheckFail(A1.unify_term(A2));
return true;
}
PREDICATE(eq2, 2)
{ return A1.unify_term(A2);
}
PREDICATE(eq3, 2)
{ return Plx_unify(A1.unwrap(), A2.unwrap());
}
PREDICATE(eq4, 2)
{ // This is what Plx_unify() expands to
return PlWrap<bool>(PL_unify(A1.unwrap(), A2.unwrap()));
}
PREDICATE(write_list, 1)
{ PlStream strm(Scurrent_output);
// This is an example of using the try...PREDICATE_CATCH for avoiding
// throwing an exception in the PlStream destructor.
PlTerm_tail tail(A1);
PlTerm_var e;
try
{ while( tail.next(e) )
strm.printf("%s\n", e.as_string().c_str());
} PREDICATE_CATCH({strm.release(); return false;})
return tail.close(); // or: PlCheckFail(tail.unify_nil());
}
PREDICATE(cappend, 3)
{ PlTerm_tail l1(A1);
PlTerm_tail l3(A3);
PlTerm_var e;
while( l1.next(e) )
PlCheckFail(l3.append(e));
return A2.unify_term(l3);
}
static const PlOptionsFlag<int>
open_query_options("open-query flag",
{ // {"debug", PL_Q_DEBUG},
// {"deterministic", PL_Q_DETERMINISTIC },
{"normal", PL_Q_NORMAL},
{"nodebug", PL_Q_NODEBUG},
{"catch_exception", PL_Q_CATCH_EXCEPTION},
{"pass_exception", PL_Q_PASS_EXCEPTION},
{"allow_exception", PL_Q_ALLOW_YIELD},
{"ext_status", PL_Q_EXT_STATUS} });
// TODO: This doesn't do quite what's expected if there's an
// exception. Instead of returning the exception to Prolog, it
// ends up in the debugger.
// Possibly this is because PlCall needs the flags
// PL_Q_CATCH_EXCEPTION and not PL_Q_PASS_EXCEPTION?
PREDICATE(cpp_call_, 3)
{ int flags = A2.as_int();
int verbose = A3.as_bool();
std::string flag_str = open_query_options.as_string(flags);
PlStream strm(Scurrent_output);
if ( flag_str.empty() )
flag_str = "cpp_call";
else
flag_str = "cpp_call(" + flag_str + ")";
if ( verbose )
strm.printf("%s: %s\n", flag_str.c_str(), A1.as_string().c_str());
try
{ int rc = PlCall(A1, flags);
if ( flags & PL_Q_EXT_STATUS )
{ const char *status_str;
switch ( rc )
{ case PL_S_EXCEPTION: status_str = "exception"; break;
case PL_S_FALSE: status_str = "false"; break;
case PL_S_TRUE: status_str = "true"; break;
case PL_S_LAST: status_str = "last"; break;
case PL_S_YIELD: status_str = "yield"; break;
default: status_str = "???"; break;
}
if ( verbose )
strm.printf("... after call, rc=%d: %s\n", rc, status_str);
} else
{ if ( verbose )
strm.printf("... after call, rc=%d\n", rc);
}
if ( rc )
{ if ( verbose )
strm.printf("cpp_call result: rc=%d: %s\n", rc, A1.as_string().c_str());
} else
{ PlTerm ex(Plx_exception(0));
if ( ex.is_null() )
{ if ( verbose )
strm.printf("cpp_call failed\n");
} else
{ if ( verbose )
strm.printf("cpp_call failed: ex: %s\n", ex.as_string().c_str());
}
}
return rc; // TODO: this is wrong with some query flags
} catch ( PlException& ex )
{ if ( ex.is_null() )
{ if ( verbose )
strm.printf("cpp_call except is_null\n");
} else
{ if ( verbose )
strm.printf("cpp_call exception: %s\n", ex.as_string().c_str());
}
throw;
}
}
PREDICATE(cpp_atom_codes, 2)
{ int rc = PlCall("atom_codes", PlTermv(A1, A2));
if ( !rc )
{ PlException ex(PlTerm(Plx_exception(0)));
PlStream strm(Scurrent_output);
if ( ex.is_null() )
strm.printf("atom_codes failed\n");
else
strm.printf("atom_codes failed: ex: %s\n", ex.as_string().c_str()); // Shouldn't happen
}
return rc;
}
/* The purpose of this predicate is mostly to show that
resource errors are dealt with appropriately: with large
enough argument, this will overflow the stacks. The Prolog
error is mapped to a C++ exception and back again when
control is passed back to Prolog. So this is just fine:
?- square_roots(1000000000, L)
ERROR: Out of global stack
*/
PREDICATE(square_roots, 2)
{ int end = A1.as_int();
PlTerm_tail list(A2);
for(int i=0; i<=end; i++)
PlCheckFail(list.append(PlTerm_float(sqrt(double(i)))));
return list.close();
}
PREDICATE(malloc_malloc, 2)
{ char *ptr = static_cast<char*>(malloc(A1.as_size_t()));
return A2.unify_pointer(ptr);
}
PREDICATE(free_malloc, 1)
{ char *ptr = static_cast<char*>(A1.as_pointer());
free(ptr);
return true;
}
PREDICATE(malloc_PL_malloc, 2)
{ char *ptr = static_cast<char*>(Plx_malloc(A1.as_size_t()));
return A2.unify_pointer(ptr);
}
PREDICATE(free_PL_malloc, 1)
{ char *ptr = static_cast<char*>(A1.as_pointer());
Plx_free(ptr);
return true;
}
PREDICATE(malloc_new, 2)
{ char *ptr = new char[A1.as_size_t()];
return A2.unify_pointer(ptr);
}
PREDICATE(free_delete, 1)
{ char *ptr = static_cast<char*>(A1.as_pointer());
delete[] ptr;
return true;
}
PREDICATE(new_chars, 2)
{ char *ptr = new char[A1.as_size_t()];
return A2.unify_pointer(ptr);
}
PREDICATE(delete_chars, 1)
{ char *ptr = static_cast<char *>(A1.as_pointer());
delete[] ptr;
return true;
}
class MyClass
{
public:
const char* contents;
MyClass() : contents("foo-bar") { }
};
PREDICATE(make_my_object, 1)
{ auto myobj = new MyClass();
return A1.unify_pointer(myobj);
}
PREDICATE(my_object_contents, 2)
{ auto myobj = static_cast<MyClass*>(A1.as_pointer());
return A2.unify_string(myobj->contents);
}
PREDICATE(free_my_object, 1)
{ auto myobj = static_cast<MyClass*>(A1.as_pointer());
delete myobj;
return true;
}
PREDICATE(make_functor, 3) // make_functor(foo, x, foo(x))
{ auto f = PlFunctor(A1.as_atom().as_string(), 1);
return A3.unify_functor(f) &&
A3[1].unify_term(A2);
}
PREDICATE(cpp_arg, 3) // like arg/3 but Arg must be instantiated
{ auto i = A1.as_uint64_t();
return A2[i].unify_term(A3);
}
PREDICATE(make_uint64, 2)
{ PlCheckFail(A2.unify_integer(A1.as_uint64_t()));
return true;
}
PREDICATE(make_int64, 2)
{ int64_t i;
// This tests PlEx<bool>
A1.get_int64_ex(&i);
PlCheckFail(A2.unify_integer(i));
return true;
}
/* The manual example uses gethostname(), but portability thereof is not
trivial and we should not introduce portability issues on tests that
are not about portability.
*/
static int
no_gethostname(char *buf, size_t len)
{ static const char hostname[] = "my_awesome_hostname";
if ( len <= 0 )
{ errno = ENAMETOOLONG;
return -1;
}
strncpy(buf, hostname, len);
if ( buf[len-1] )
{ errno = ENAMETOOLONG;
return -1;
}
return 0;
}
PREDICATE(hostname, 1)
{ char buf[255+1]; // SYSv2; POSIX.1 has a smaller HOST_NAME_MAX+1
if ( no_gethostname(buf, sizeof buf) == 0 )
return A1.unify_atom(buf);
return false;
}
PREDICATE(hostname2, 1)
{ char buf[255+1]; // SYSv2; POSIX.1 has a smaller HOST_NAME_MAX+1
if ( no_gethostname(buf, sizeof buf) != 0 )
throw PlFail();
PlCheckFail(A1.unify_atom(buf));
return true;
}
// The eq_int64/2 and lt_int64/2 predicates test the deprecated PlTerm::operator==()
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning (disable:4996)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
PREDICATE(eq_int64, 2)
{ return A1 == A2.as_int64_t();
}
PREDICATE(lt_int64, 2)
{ return A1 < A2.as_int64_t();
}
#ifdef _MSC_VER
#pragma warning( pop )
#else
#pragma GCC diagnostic pop
#endif
PREDICATE(get_atom_ex, 2)
{ PlAtom a(PlAtom::null);
A1.get_atom_ex(&a);
return A2.unify_atom(a);
}
PREDICATE(ensure_PlTerm_forward_declarations_are_implemented, 0)
{ /*********************************************************************
* This code is not intended to be executed; it is only compiled, to *
* check that implementations exist where expected. *
*********************************************************************/
PlTerm_var t_var;
PlTerm_atom t_atom1("abc");
PlTerm_atom t_atom2(L"ABC");
PlTerm_atom t_atom3(PlAtom("an atom"));
PlTerm_atom p_atom4(std::string("abc"));
PlTerm_atom p_atom5(std::wstring(L"世界"));
PlTerm_term_t t_t(Plx_new_term_ref());
PlTerm_term_t t_null(PlTerm::null);
PlTerm t_t2(Plx_new_term_ref());
PlTerm t_null2(PlTerm::null);
// The various integer types are also used in IntInfo.
PlTerm_integer t_int1(std::numeric_limits<int>::max());
PlTerm_integer t_int1b(std::numeric_limits<int>::min());
PlTerm_integer t_int2(std::numeric_limits<long>::max());
PlTerm_integer t_int2b(std::numeric_limits<long>::min());
PlTerm_integer t_int64(std::numeric_limits<int64_t>::max());
PlTerm_integer t_int64b(std::numeric_limits<int64_t>::min());
PlTerm_integer t_uint64(std::numeric_limits<uint64_t>::max());
PlTerm_integer t_uint64b(std::numeric_limits<uint64_t>::min());
PlTerm_integer p_size(static_cast<size_t>(-1));
PlTerm_integer p_size2(std::numeric_limits<size_t>::max());
PlTerm_float t_float(1.23);
PlTerm_pointer t_ptr(&t_var);
// There's a better test for PlRecord in int_info/2
PlRecord r_atom1(t_atom1.record());
PlCheckFail(t_atom1.unify_term(r_atom1.term()));
std::shared_ptr<PlRecord> r_atom2_p(new PlRecord(t_atom2.record()), PlRecordDeleter());
PlCheckFail(t_atom2.unify_term(r_atom2_p->term()));
PlTerm t_rec(r_atom2_p->term());
PlCheckFail(t_rec.unify_term(t_atom2));
PlTerm_string t_string1("abc");
PlTerm_string t_string2(L"世界");
const char codes[] = {81,82,83,0};
PlTerm_list_codes s02(codes);
PlTerm_list_chars s03("mno");
PlTerm_var tt;
tt.put_variable();
tt.put_atom(PlAtom("xyz"));
tt.put_bool(false);
tt.put_atom_chars("abcdefg");
tt.put_string_chars("gfedcba");
tt.put_chars(0, 3, "abc");
tt.put_list_chars("mnopq");
tt.put_list_codes("1234");
tt.put_atom_nchars(3, "111");
tt.put_string_nchars(3, "222");
tt.put_list_nchars(3, "333");
tt.put_list_ncodes(3, "444");
tt.put_integer(-1234);
tt.put_pointer(&tt);
tt.put_float(0.123);
tt.put_functor(PlFunctor("foo", 1));
tt.put_list();
tt.put_nil();
tt.put_term(t_string1);
PlAtom atom1("atom1");
PlAtom atom2(L"原子2");
PlAtom atom3(std::string("atom3"));
PlAtom atom4(std::wstring(L"原子4"));
PlAtom a5a(t_atom1.as_atom());
PlAtom atom_null(PlAtom::null);
// The following are unsafe (the as_string() is deleted in the statement):
// const char * x01 = t_var.as_string().c_str();
// const wchar_t *x01a = t_var.as_wstring().c_str();
const std::string s01 = atom3.as_string();
const std::wstring s01b = atom4.as_wstring();
const std::string s02a = t_var.as_string();
const std::wstring s02b = t_var.as_wstring();
atom1.register_ref();
atom1.unregister_ref();
{ int v1;
unsigned v2;
long v3;
unsigned long v4;
size_t v5;
t_int1.integer(&v1);
t_int1.integer(&v2);
t_int1.integer(&v3);
t_int1.integer(&v4);
t_int1.integer(&v5);
}
long x04 = t_atom2.as_long();
int x05 = t_int1.as_int();
uint32_t x06 = t_var.as_uint32_t();
uint64_t x07 = t_var.as_uint64_t();
int64_t x08 = t_var.as_int64_t();
size_t x09 = t_var.as_size_t();
bool x10 = t_var.as_bool();
double x11 = t_var.as_float();
double x12 = t_var.as_double();
PlAtom x13 = t_var.as_atom();
void * x14 = t_var.as_pointer();
PlTerm x20 = t_var[1];
size_t x21 = t_var.arity();
PlAtom x22 = t_var.name();
//(void)x01;
//(void)x01a;
(void)a5a;
(void)x04;
(void)x05;
(void)x06;
(void)x07;
(void)x08;
(void)x09;
(void)x10;
(void)x11;
(void)x12;
(void)x13;
(void)x14;
(void)x20;
(void)x21;
(void)x22;
(void)t_var.unify_term(t_atom1);
(void)t_var.unify_atom(PlAtom("an atom"));
(void)t_atom1.unify_atom("abc");
(void)t_atom2.unify_atom(L"ABC");
(void)t_atom3.unify_functor(PlFunctor("f", 3));
(void)t_int1.unify_integer(123);
(void)t_int2.unify_integer(666);
(void)t_int2b.unify_integer(0);
(void)p_size.unify_integer(sizeof t_var);
(void)t_float.unify_float(1.23);
(void)t_ptr.unify_pointer(&t_var);
bool xx01;
char xx02;
signed char xx03;
unsigned char xx04;
// TODO:
// wchar_t xx05;
// char16_t xx06;
// char32_t xx07;
short xx08;
unsigned short xx09;
int xx10;
unsigned int xx11;
long xx12;
unsigned long xx13;
long long xx14;
unsigned long long xx15;
size_t xx16;
int32_t xx17;
uint32_t xx18;
uint64_t xx19;
int64_t xx20;
intptr_t xx21;
uintptr_t xx22;
t_int1.integer(&xx01);
t_int1.integer(&xx02);
t_int1.integer(&xx03);
t_int1.integer(&xx04);
// TODO:
// t_int1.integer(&xx05);
// t_int1.integer(&xx06);
// t_int1.integer(&xx07);
t_int1.integer(&xx08);
t_int1.integer(&xx09);
t_int1.integer(&xx10);
t_int1.integer(&xx11);
t_int1.integer(&xx12);
t_int1.integer(&xx13);
t_int1.integer(&xx14);
t_int1.integer(&xx15);
t_int1.integer(&xx16);
t_int1.integer(&xx17);
t_int1.integer(&xx18);
t_int1.integer(&xx19);
t_int1.integer(&xx20);
t_int1.integer(&xx21);
t_int1.integer(&xx22);
PlStream strm(x20, 0);
strm.set_timeout(1);
strm.unit_size();
strm.canrepresent('a');
strm.putcode('x');
strm.getcode();
strm.putw(13);
strm.getw();
char data[10];
size_t bytes = strm.fwrite("abc", sizeof (char), 3);
bytes = strm.fread(data, sizeof data[0], bytes);
if ( strm.feof() ) return false;
if ( strm.fpasteof() ) return false;
strm.clearerr();
// TODO: the rest of the methods
strm.release();
return true;
}
PREDICATE(unify_int_set, 1)
{ int i_int = 0;
unsigned i_unsigned = 0;
long i_long = 0;
unsigned long i_unsigned_long = 0;
size_t i_size = 0;
int32_t i_int32 = 0;
uint32_t i_uint32 = 0;
int64_t i_int64 = 0;
uint64_t i_uint64 = 0;
PlCheckFail(A1.unify_integer(i_int));
PlCheckFail(A1.unify_integer(i_unsigned));
PlCheckFail(A1.unify_integer(i_long));
PlCheckFail(A1.unify_integer(i_unsigned_long));
PlCheckFail(A1.unify_integer(i_size));
PlCheckFail(A1.unify_integer(i_int32));
PlCheckFail(A1.unify_integer(i_uint32));
PlCheckFail(A1.unify_integer(i_int64));
PlCheckFail(A1.unify_integer(i_uint64));
return true;
}
// The following are for verifying some documentation details.
PREDICATE(c_PL_unify_nil, 1) { return static_cast<foreign_t>(PL_unify_nil(A1.unwrap())); }
PREDICATE(cpp_unify_nil, 1) { return A1.unify_nil(); }
PREDICATE(check_c_PL_unify_nil, 1) { PlEx<bool>(PL_unify_nil(A1.unwrap())); return true; }
// Repeat the above, for *_ex():
PREDICATE(c_PL_unify_nil_ex, 1) { return static_cast<foreign_t>(PL_unify_nil_ex(A1.unwrap())); }
PREDICATE(cpp_unify_nil_ex, 1) { A1.unify_nil_ex(); return true; }
PREDICATE(check_c_PL_unify_nil_ex, 1) { PlEx<bool>(PL_unify_nil_ex(A1.unwrap())); return true; }
PREDICATE(c_PL_get_nil, 1) { return static_cast<foreign_t>(PL_get_nil(A1.unwrap())); }
PREDICATE(cpp_as_nil, 1) { A1.as_nil(); return true; }
PREDICATE(check_c_PL_get_nil, 1) { PlEx<bool>(PL_get_nil(A1.unwrap())); return true; }
PREDICATE(check_c_PL_get_nil_ex, 1) { PlEx<bool>(PL_get_nil_ex(A1.unwrap())); return true; }
// Functions re-implemented from ffi4pl.c
// range_cpp/3 is similar to range_ffialloc/3
/* range_cpp/3 is used in regression tests:
- PL_foreign_context_address() and malloc()-ed context.
*/
struct RangeCtxt
{ long i;
long high;
explicit RangeCtxt(long i, long high)
: i(i), high(high) { }
};
PREDICATE_NONDET(range_cpp, 3)
{ auto t_low = A1, t_high = A2, t_result = A3;
auto ctxt = handle.context_unique_ptr<RangeCtxt>();
switch( handle.foreign_control() )
{ case PL_FIRST_CALL:
ctxt.reset(new RangeCtxt(t_low.as_long(), t_high.as_long()));
break;
case PL_REDO:
break;
case PL_PRUNED:
return true;
default:
assert(0);
return false;
}
if ( ctxt->i >= ctxt->high ||
!t_result.unify_integer(ctxt->i) )
return false;
ctxt->i += 1;
if ( ctxt->i >= ctxt->high )
{ return true; // Last result: succeed without a choice point
}
PL_retry_address(ctxt.release()); // Succeed with a choice point
}
// For benchmarking `throw PlThrow()` vs `return false`
// Times are given for 1 million failures