File indexing completed on 2025-05-11 08:24:34
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 #include <stdio.h>
0045 #include <string.h>
0046 #include <unistd.h>
0047
0048 #include <rtems.h>
0049 #include <rtems/test-info.h>
0050 #include <tmacros.h>
0051
0052 #include <rtems/regulator.h>
0053
0054
0055
0056
0057 const char rtems_test_name[] = "Regulator 1";
0058
0059
0060
0061
0062 void *__wrap_malloc(size_t size);
0063 void *__real_malloc(size_t size);
0064
0065
0066
0067
0068
0069 static int malloc_trigger_count;
0070
0071
0072
0073
0074
0075 static int malloc_call_count;
0076
0077
0078
0079
0080
0081 static bool malloc_trigger_enabled;
0082
0083
0084
0085
0086
0087 static void malloc_trigger_enable(
0088 int trigger_count
0089 )
0090 {
0091 malloc_trigger_enabled = true;
0092 malloc_trigger_count = trigger_count;
0093 malloc_call_count = 0;
0094 }
0095
0096
0097
0098
0099 static void malloc_trigger_reset(void)
0100 {
0101 malloc_trigger_enabled = 0;
0102 malloc_trigger_count = 0;
0103 malloc_call_count = 0;
0104 }
0105
0106
0107
0108
0109 void *__wrap_malloc(size_t size)
0110 {
0111 if (malloc_trigger_enabled) {
0112 malloc_call_count++;
0113 if (malloc_call_count == malloc_trigger_count) {
0114 return NULL;
0115 }
0116 }
0117
0118 return __real_malloc(size);
0119 }
0120
0121
0122
0123
0124 #define FIVE_SECONDS (5 * rtems_clock_get_ticks_per_second())
0125
0126
0127
0128
0129
0130 static bool test_regulator_deliverer(
0131 void *context,
0132 void *message,
0133 size_t length
0134 )
0135 {
0136 (void) context;
0137 (void) message;
0138 (void) length;
0139
0140 return true;
0141 }
0142
0143
0144
0145
0146
0147 #define MAXIMUM_MESSAGE_LENGTH 32
0148
0149
0150
0151
0152
0153 #define MAXIMUM_MESSAGES_TO_BUFFER 10
0154
0155
0156
0157
0158
0159 typedef struct {
0160 rtems_interval processed;
0161 char message[MAXIMUM_MESSAGE_LENGTH];
0162 } message_log_t;
0163
0164
0165
0166
0167
0168 message_log_t delivered_messages[MAXIMUM_MESSAGES_TO_BUFFER];
0169
0170
0171
0172
0173
0174 int delivered_message_count;
0175
0176
0177
0178
0179
0180
0181
0182
0183 static void delivered_messages_reset(void)
0184 {
0185 delivered_message_count = 0;
0186 memset(delivered_messages, 0xc5, sizeof(delivered_messages));
0187 }
0188
0189
0190
0191
0192 typedef struct {
0193
0194 rtems_regulator_instance *regulator;
0195 } deliverer_logger_context_t;
0196
0197
0198
0199
0200 static deliverer_logger_context_t deliverer_logger_context;
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210 static bool test_regulator_deliverer_logger(
0211 void *context,
0212 void *message,
0213 size_t length
0214 )
0215 {
0216 deliverer_logger_context_t *the_context;
0217
0218 the_context = (deliverer_logger_context_t *)context;
0219
0220 static bool caller_releases_buffer = true;
0221
0222 size_t len;
0223 rtems_interval ticks;
0224 rtems_status_code sc;
0225
0226 len = strnlen(message, MAXIMUM_MESSAGE_LENGTH) + 1;
0227 rtems_test_assert(len = length);
0228
0229 ticks = rtems_clock_get_ticks_since_boot();
0230
0231 delivered_messages[delivered_message_count].processed = ticks;
0232
0233 strcpy(delivered_messages[delivered_message_count].message, message);
0234
0235 delivered_message_count++;
0236
0237
0238
0239
0240
0241 if (caller_releases_buffer == true) {
0242 caller_releases_buffer = false;
0243 return true;
0244 }
0245
0246 sc = rtems_regulator_release_buffer(the_context->regulator, message);
0247 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0248
0249 return false;
0250 }
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 static rtems_regulator_instance *test_regulator_create_regulator_OK(void)
0262 {
0263 rtems_status_code sc;
0264 rtems_regulator_attributes attributes = {
0265 .deliverer = test_regulator_deliverer,
0266 .deliverer_context = NULL,
0267 .maximum_message_size = 16,
0268 .maximum_messages = 10,
0269 .delivery_thread_priority = 16,
0270 .delivery_thread_stack_size = 0,
0271 .delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000),
0272 .maximum_to_dequeue_per_period = 3
0273 };
0274 rtems_regulator_instance *regulator;
0275
0276 regulator = NULL;
0277
0278 sc = rtems_regulator_create(&attributes, ®ulator);
0279 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0280 rtems_test_assert(regulator != NULL);
0281
0282 return regulator;
0283 }
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293 static void test_regulator_create_max_dequeue_zero(void)
0294 {
0295 rtems_status_code sc;
0296 rtems_regulator_attributes attributes = {
0297 .deliverer = test_regulator_deliverer,
0298 .deliverer_context = NULL,
0299 .maximum_message_size = 16,
0300 .maximum_messages = 10,
0301 .delivery_thread_priority = 16,
0302 .delivery_thread_stack_size = 0,
0303 .delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000),
0304 .maximum_to_dequeue_per_period = 0
0305 };
0306 rtems_regulator_instance *regulator;
0307
0308 regulator = NULL;
0309
0310 sc = rtems_regulator_create(&attributes, ®ulator);
0311 rtems_test_assert(sc == RTEMS_INVALID_NUMBER);
0312 }
0313
0314
0315
0316
0317
0318
0319
0320
0321 static void test_regulator_create_null_attributes(void)
0322 {
0323 rtems_status_code sc;
0324 rtems_regulator_instance *regulator;
0325
0326 sc = rtems_regulator_create(NULL, ®ulator);
0327 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0328 }
0329
0330
0331
0332
0333
0334
0335
0336
0337 static void test_regulator_create_null_regulator(void)
0338 {
0339 rtems_status_code sc;
0340 rtems_regulator_attributes attributes;
0341
0342 sc = rtems_regulator_create(&attributes, NULL);
0343 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0344 }
0345
0346
0347
0348
0349
0350
0351
0352
0353 static void test_regulator_create_deliverer_is_null(void)
0354 {
0355 rtems_status_code sc;
0356 rtems_regulator_attributes attributes;
0357 rtems_regulator_instance *regulator;
0358
0359 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0360
0361 attributes.deliverer = NULL;
0362 attributes.maximum_messages = 0;
0363 attributes.maximum_message_size = 16;
0364 attributes.maximum_to_dequeue_per_period = 1;
0365
0366 sc = rtems_regulator_create(&attributes, ®ulator);
0367 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0368 }
0369
0370
0371
0372
0373
0374
0375
0376
0377 static void test_regulator_create_maximum_messages_is_zero(void)
0378 {
0379 rtems_status_code sc;
0380 rtems_regulator_attributes attributes;
0381 rtems_regulator_instance *regulator;
0382
0383 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0384
0385 attributes.deliverer = test_regulator_deliverer;
0386 attributes.maximum_messages = 0;
0387 attributes.maximum_message_size = 16;
0388 attributes.maximum_to_dequeue_per_period = 1;
0389
0390 sc = rtems_regulator_create(&attributes, ®ulator);
0391 rtems_test_assert(sc == RTEMS_INVALID_NUMBER);
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401 static void test_regulator_create_maximum_message_size_is_zero(void)
0402 {
0403 rtems_status_code sc;
0404 rtems_regulator_attributes attributes;
0405 rtems_regulator_instance *regulator;
0406
0407 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0408
0409 attributes.deliverer = test_regulator_deliverer;
0410 attributes.maximum_messages = 10;
0411 attributes.maximum_message_size = 0;
0412 attributes.maximum_to_dequeue_per_period = 1;
0413
0414 sc = rtems_regulator_create(&attributes, ®ulator);
0415 rtems_test_assert(sc == RTEMS_INVALID_SIZE);
0416 }
0417
0418
0419
0420
0421
0422
0423
0424
0425 static void test_regulator_create_maximum_to_dequeue_per_period_is_zero(void)
0426 {
0427 rtems_status_code sc;
0428 rtems_regulator_attributes attributes;
0429 rtems_regulator_instance *regulator;
0430
0431 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0432
0433 attributes.deliverer = test_regulator_deliverer;
0434 attributes.maximum_messages = 10;
0435 attributes.maximum_message_size = 0;
0436
0437 sc = rtems_regulator_create(&attributes, ®ulator);
0438 rtems_test_assert(sc == RTEMS_INVALID_SIZE);
0439 }
0440
0441
0442
0443
0444
0445
0446
0447
0448 static void test_regulator_create_malloc_regulator_fails(void)
0449 {
0450 rtems_status_code sc;
0451 rtems_regulator_attributes attributes;
0452 rtems_regulator_instance *regulator;
0453
0454 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0455
0456 attributes.deliverer = test_regulator_deliverer;
0457 attributes.maximum_messages = 10;
0458 attributes.maximum_message_size = 16;
0459 attributes.delivery_thread_priority = 32;
0460 attributes.maximum_to_dequeue_per_period = 1;
0461 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0462
0463 malloc_trigger_enable(1);
0464
0465 sc = rtems_regulator_create(&attributes, ®ulator);
0466 rtems_test_assert(sc == RTEMS_NO_MEMORY);
0467
0468 malloc_trigger_reset();
0469 }
0470
0471
0472
0473
0474
0475
0476
0477
0478 static void test_regulator_create_malloc_buffers_fails(void)
0479 {
0480 rtems_status_code sc;
0481 rtems_regulator_attributes attributes;
0482 rtems_regulator_instance *regulator;
0483
0484 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0485
0486 attributes.deliverer = test_regulator_deliverer;
0487 attributes.maximum_messages = 10;
0488 attributes.maximum_message_size = 16;
0489 attributes.delivery_thread_priority = 32;
0490 attributes.maximum_to_dequeue_per_period = 1;
0491 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0492
0493 malloc_trigger_enable(2);
0494
0495 sc = rtems_regulator_create(&attributes, ®ulator);
0496 rtems_test_assert(sc == RTEMS_NO_MEMORY);
0497
0498 malloc_trigger_reset();
0499 }
0500
0501
0502
0503
0504
0505
0506
0507
0508 static void test_regulator_create_delivery_thread_priority_is_zero(void)
0509 {
0510 rtems_status_code sc;
0511 rtems_regulator_attributes attributes;
0512 rtems_regulator_instance *regulator;
0513
0514 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0515
0516 attributes.deliverer = test_regulator_deliverer;
0517 attributes.maximum_messages = 10;
0518 attributes.maximum_message_size = 16;
0519 attributes.delivery_thread_priority = 0;
0520 attributes.maximum_to_dequeue_per_period = 1;
0521 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0522
0523 sc = rtems_regulator_create(&attributes, ®ulator);
0524 rtems_test_assert(sc == RTEMS_INVALID_PRIORITY);
0525 }
0526
0527
0528
0529
0530
0531
0532
0533
0534 static void test_regulator_create_partition_create_fails(void)
0535 {
0536 rtems_status_code sc;
0537 rtems_id partition_id;
0538 unsigned long partition_area[16];
0539 rtems_regulator_attributes attributes;
0540 rtems_regulator_instance *regulator;
0541
0542 sc = rtems_partition_create(
0543 rtems_build_name('T', 'P', 'T', 'P'),
0544 partition_area,
0545 16 * sizeof(unsigned long),
0546 2 * sizeof(unsigned long),
0547 RTEMS_DEFAULT_ATTRIBUTES,
0548 &partition_id
0549 );
0550 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0551
0552 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0553
0554 attributes.deliverer = test_regulator_deliverer;
0555 attributes.maximum_messages = 10;
0556 attributes.maximum_message_size = 16;
0557 attributes.delivery_thread_priority = 8;
0558 attributes.maximum_to_dequeue_per_period = 1;
0559 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0560
0561 sc = rtems_regulator_create(&attributes, ®ulator);
0562 rtems_test_assert(sc == RTEMS_TOO_MANY);
0563
0564 sc = rtems_partition_delete(partition_id);
0565 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0566 }
0567
0568
0569
0570
0571
0572
0573
0574
0575 static void test_regulator_create_message_queue_create_fails(void)
0576 {
0577 rtems_status_code sc;
0578 rtems_id queue_id;
0579 rtems_regulator_attributes attributes;
0580 rtems_regulator_instance *regulator;
0581
0582 sc = rtems_message_queue_create(
0583 rtems_build_name('T', 'Q', 'T', 'Q'),
0584 4,
0585 sizeof(unsigned long),
0586 RTEMS_DEFAULT_ATTRIBUTES,
0587 &queue_id
0588 );
0589 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0590
0591 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0592
0593 attributes.deliverer = test_regulator_deliverer;
0594 attributes.maximum_messages = 10;
0595 attributes.maximum_message_size = 16;
0596 attributes.delivery_thread_priority = 8;
0597 attributes.maximum_to_dequeue_per_period = 1;
0598 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0599
0600 sc = rtems_regulator_create(&attributes, ®ulator);
0601 rtems_test_assert(sc == RTEMS_TOO_MANY);
0602
0603 sc = rtems_message_queue_delete(queue_id);
0604 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0605 }
0606
0607
0608
0609
0610
0611
0612
0613
0614 static void test_regulator_create_task_create_fails(void)
0615 {
0616 rtems_status_code sc;
0617 rtems_id task_id;
0618 rtems_regulator_attributes attributes;
0619 rtems_regulator_instance *regulator;
0620
0621 sc = rtems_task_create(
0622 rtems_build_name('T', 'T', 'T', 'T'),
0623 80,
0624 0,
0625 RTEMS_DEFAULT_MODES,
0626 RTEMS_DEFAULT_ATTRIBUTES,
0627 &task_id
0628 );
0629 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0630
0631 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0632
0633 attributes.deliverer = test_regulator_deliverer;
0634 attributes.maximum_messages = 10;
0635 attributes.maximum_message_size = 16;
0636 attributes.delivery_thread_priority = 8;
0637 attributes.maximum_to_dequeue_per_period = 1;
0638 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0639
0640 sc = rtems_regulator_create(&attributes, ®ulator);
0641 rtems_test_assert(sc == RTEMS_TOO_MANY);
0642
0643 sc = rtems_task_delete(task_id);
0644 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0645 }
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657 static void test_regulator_create_rate_monotonic_create_fails(void)
0658 {
0659 rtems_status_code sc;
0660 rtems_id period_id;
0661 rtems_regulator_attributes attributes;
0662 rtems_regulator_instance *regulator;
0663
0664 sc = rtems_rate_monotonic_create(
0665 rtems_build_name('T', 'S', 'T', 'P'),
0666 &period_id
0667 );
0668 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0669
0670 (void) memset(&attributes, 0, sizeof(rtems_regulator_attributes));
0671
0672 attributes.deliverer = test_regulator_deliverer;
0673 attributes.maximum_messages = 10;
0674 attributes.maximum_message_size = 16;
0675 attributes.delivery_thread_priority = 8;
0676 attributes.maximum_to_dequeue_per_period = 1;
0677 attributes.delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000);
0678
0679 sc = rtems_regulator_create(&attributes, ®ulator);
0680 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0681
0682
0683
0684
0685
0686 sleep(1);
0687
0688
0689
0690
0691 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
0692 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0693
0694 sc = rtems_rate_monotonic_delete(period_id);
0695 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0696 }
0697
0698
0699
0700
0701
0702
0703
0704
0705 static void test_regulator_delete_null_regulator(void)
0706 {
0707 rtems_status_code sc;
0708
0709 sc = rtems_regulator_delete(NULL, FIVE_SECONDS);
0710 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0711 }
0712
0713
0714
0715
0716
0717
0718
0719
0720 static void test_regulator_delete_uninitialized_regulator(void)
0721 {
0722 rtems_status_code sc;
0723 rtems_regulator_instance regulator;
0724
0725 (void) memset(®ulator, 0, sizeof(regulator));
0726
0727 sc = rtems_regulator_delete(®ulator, 0);
0728 rtems_test_assert(sc == RTEMS_INCORRECT_STATE);
0729 }
0730
0731
0732
0733
0734
0735
0736
0737
0738 static void test_regulator_delete_OK(void)
0739 {
0740 rtems_status_code sc;
0741 rtems_regulator_instance *regulator;
0742
0743 regulator = test_regulator_create_regulator_OK();
0744 rtems_test_assert(regulator != NULL);
0745
0746 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
0747 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0748 }
0749
0750
0751
0752
0753
0754
0755
0756
0757 static void test_regulator_obtain_buffer_null_regulator(void)
0758 {
0759 rtems_status_code sc;
0760 void *buffer;
0761
0762 sc = rtems_regulator_obtain_buffer(NULL, &buffer);
0763 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0764 }
0765
0766
0767
0768
0769
0770
0771
0772
0773 static void test_regulator_obtain_buffer_uninitialized_regulator(void)
0774 {
0775 rtems_status_code sc;
0776 rtems_regulator_instance regulator;
0777 void *buffer;
0778
0779 (void) memset(®ulator, 0, sizeof(regulator));
0780
0781 sc = rtems_regulator_obtain_buffer(®ulator, &buffer);
0782 rtems_test_assert(sc == RTEMS_INCORRECT_STATE);
0783 }
0784
0785
0786
0787
0788
0789
0790
0791
0792 static void test_regulator_obtain_buffer_OK(void)
0793 {
0794 rtems_status_code sc;
0795 rtems_regulator_instance *regulator;
0796 void *buffer;
0797
0798 regulator = test_regulator_create_regulator_OK();
0799 rtems_test_assert(regulator != NULL);
0800
0801 sc = rtems_regulator_obtain_buffer(regulator, &buffer);
0802 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0803 rtems_test_assert(buffer != NULL);
0804
0805
0806
0807
0808
0809 sc = rtems_regulator_release_buffer(regulator, buffer);
0810 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0811 rtems_test_assert(buffer != NULL);
0812
0813 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
0814 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0815 }
0816
0817
0818
0819
0820
0821
0822
0823
0824 static void test_regulator_release_buffer_null_regulator(void)
0825 {
0826 rtems_status_code sc;
0827 void *buffer;
0828
0829 sc = rtems_regulator_release_buffer(NULL, &buffer);
0830 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0831 }
0832
0833
0834
0835
0836
0837
0838
0839
0840 static void test_regulator_release_buffer_uninitialized_regulator(void)
0841 {
0842 rtems_status_code sc;
0843 rtems_regulator_instance regulator;
0844 void *buffer;
0845
0846 (void) memset(®ulator, 0, sizeof(regulator));
0847
0848 sc = rtems_regulator_release_buffer(®ulator, &buffer);
0849 rtems_test_assert(sc == RTEMS_INCORRECT_STATE);
0850 }
0851
0852
0853
0854
0855
0856
0857
0858
0859 static void test_regulator_release_buffer_OK(void)
0860 {
0861 rtems_status_code sc;
0862 rtems_regulator_instance *regulator;
0863 void *buffer;
0864
0865 regulator = test_regulator_create_regulator_OK();
0866 rtems_test_assert(regulator != NULL);
0867
0868 sc = rtems_regulator_obtain_buffer(regulator, &buffer);
0869 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0870 rtems_test_assert(buffer != NULL);
0871
0872 sc = rtems_regulator_release_buffer(regulator, buffer);
0873 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0874
0875 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
0876 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0877 }
0878
0879
0880
0881
0882
0883
0884
0885
0886 static void test_regulator_send_null_regulator(void)
0887 {
0888 rtems_status_code sc;
0889 void *buffer;
0890 size_t length;
0891
0892 buffer = &length;
0893 length = sizeof(size_t);
0894
0895 sc = rtems_regulator_send(NULL, buffer, length);
0896 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0897 }
0898
0899
0900
0901
0902
0903
0904
0905
0906 static void test_regulator_send_null_message(void)
0907 {
0908 rtems_status_code sc;
0909 size_t length;
0910 rtems_regulator_instance regulator;
0911
0912 length = sizeof(size_t);
0913
0914 sc = rtems_regulator_send(®ulator, NULL, length);
0915 rtems_test_assert(sc == RTEMS_INVALID_ADDRESS);
0916 }
0917
0918
0919
0920
0921
0922
0923
0924
0925 static void test_regulator_send_length_is_zero(void)
0926 {
0927 rtems_status_code sc;
0928 rtems_regulator_instance regulator;
0929 void *buffer;
0930 size_t length;
0931
0932 buffer = &length;
0933 length = 0;
0934
0935 sc = rtems_regulator_send(®ulator, buffer, length);
0936 rtems_test_assert(sc == RTEMS_INVALID_NUMBER);
0937 }
0938
0939
0940
0941
0942
0943
0944
0945
0946 static void test_regulator_send_uninitialized_regulator(void)
0947 {
0948 rtems_status_code sc;
0949 rtems_regulator_instance regulator;
0950 void *buffer;
0951 size_t length;
0952
0953 buffer = &length;
0954 length = sizeof(size_t);
0955
0956 (void) memset(®ulator, 0, sizeof(regulator));
0957
0958 sc = rtems_regulator_send(®ulator, buffer, length);
0959 rtems_test_assert(sc == RTEMS_INCORRECT_STATE);
0960 }
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970 static void test_regulator_cannot_delete_with_outstanding(void)
0971 {
0972 rtems_status_code sc;
0973 rtems_regulator_instance *regulator;
0974 char message[MAXIMUM_MESSAGE_LENGTH];
0975 void *buffer;
0976 size_t length;
0977 int match;
0978 rtems_regulator_attributes attributes = {
0979 .deliverer = test_regulator_deliverer_logger,
0980 .deliverer_context = &deliverer_logger_context,
0981 .maximum_message_size = 16,
0982 .maximum_messages = 10,
0983 .delivery_thread_priority = 16,
0984 .delivery_thread_stack_size = 0,
0985 .delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(250),
0986 .maximum_to_dequeue_per_period = 3
0987 };
0988 rtems_regulator_statistics stats;
0989
0990 sc = rtems_regulator_create(&attributes, ®ulator);
0991 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
0992 rtems_test_assert(regulator != NULL);
0993
0994 deliverer_logger_context.regulator = regulator;
0995
0996 delivered_messages_reset();
0997
0998
0999 sc = rtems_regulator_get_statistics(regulator, &stats);
1000 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1001 rtems_test_assert(stats.obtained == 0);
1002 rtems_test_assert(stats.released == 0);
1003 rtems_test_assert(stats.delivered == 0);
1004 rtems_test_assert(stats.period_statistics.count == 0);
1005 rtems_test_assert(stats.period_statistics.missed_count == 0);
1006
1007
1008 sc = rtems_regulator_obtain_buffer(regulator, &buffer);
1009 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1010 rtems_test_assert(buffer != NULL);
1011
1012
1013 sc = rtems_regulator_get_statistics(regulator, &stats);
1014 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1015 rtems_test_assert(stats.obtained == 1);
1016 rtems_test_assert(stats.released == 0);
1017 rtems_test_assert(stats.delivered == 0);
1018 rtems_test_assert(stats.period_statistics.count == 0);
1019 rtems_test_assert(stats.period_statistics.missed_count == 0);
1020
1021
1022
1023 length = snprintf(message, MAXIMUM_MESSAGE_LENGTH, "message %d", 1024) + 1;
1024 strcpy(buffer, message);
1025
1026 sc = rtems_regulator_send(regulator, buffer, length);
1027 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1028
1029
1030 sc = rtems_regulator_get_statistics(regulator, &stats);
1031 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1032 rtems_test_assert(stats.obtained == 1);
1033 rtems_test_assert(stats.released == 0);
1034 rtems_test_assert(stats.delivered == 0);
1035 rtems_test_assert(stats.period_statistics.count == 0);
1036 rtems_test_assert(stats.period_statistics.missed_count == 0);
1037
1038
1039 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
1040 rtems_test_assert(sc == RTEMS_RESOURCE_IN_USE);
1041
1042
1043 sleep(1);
1044
1045
1046 sc = rtems_regulator_get_statistics(regulator, &stats);
1047 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1048 rtems_test_assert(stats.obtained == 1);
1049 rtems_test_assert(stats.released == 1);
1050 rtems_test_assert(stats.delivered == 1);
1051 rtems_test_assert(stats.period_statistics.count != 0);
1052 rtems_test_assert(stats.period_statistics.missed_count == 0);
1053
1054 rtems_test_assert(delivered_message_count == 1);
1055 match = strncmp(
1056 delivered_messages[0].message,
1057 message,
1058 MAXIMUM_MESSAGE_LENGTH
1059 );
1060 rtems_test_assert(match == 0);
1061
1062 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
1063 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1064
1065 deliverer_logger_context.regulator = NULL;
1066 }
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 static void test_regulator_send_one_message_OK(void)
1077 {
1078 rtems_status_code sc;
1079 rtems_regulator_instance *regulator;
1080 char message[MAXIMUM_MESSAGE_LENGTH];
1081 void *buffer;
1082 size_t length;
1083 int match;
1084 rtems_regulator_attributes attributes = {
1085 .deliverer = test_regulator_deliverer_logger,
1086 .deliverer_context = &deliverer_logger_context,
1087 .maximum_message_size = 16,
1088 .maximum_messages = 10,
1089 .delivery_thread_priority = 16,
1090 .delivery_thread_stack_size = 0,
1091 .delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(250),
1092 .maximum_to_dequeue_per_period = 3
1093 };
1094
1095 sc = rtems_regulator_create(&attributes, ®ulator);
1096 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1097 rtems_test_assert(regulator != NULL);
1098
1099 deliverer_logger_context.regulator = regulator;
1100
1101 delivered_messages_reset();
1102
1103 sc = rtems_regulator_obtain_buffer(regulator, &buffer);
1104 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1105 rtems_test_assert(buffer != NULL);
1106
1107 length = snprintf(message, MAXIMUM_MESSAGE_LENGTH, "message %d", 1024) + 1;
1108 strcpy(buffer, message);
1109
1110 sc = rtems_regulator_send(regulator, buffer, length);
1111 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1112
1113 sleep(1);
1114
1115 rtems_test_assert(delivered_message_count == 1);
1116 match = strncmp(
1117 delivered_messages[0].message,
1118 message,
1119 MAXIMUM_MESSAGE_LENGTH
1120 );
1121 rtems_test_assert(match == 0);
1122
1123 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
1124 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1125
1126 deliverer_logger_context.regulator = NULL;
1127 }
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137 #include <stdio.h>
1138 static void test_regulator_send_multiple_messages_OK(void)
1139 {
1140 rtems_status_code sc;
1141 rtems_regulator_instance *regulator;
1142 char message[MAXIMUM_MESSAGE_LENGTH];
1143 void *buffer;
1144 size_t length;
1145 int match;
1146 int i;
1147 time_t base_time;
1148 time_t tmp_time;
1149 rtems_interval base_ticks;
1150 rtems_interval ticks;
1151 rtems_interval ticks_per_second;
1152
1153 rtems_regulator_attributes attributes = {
1154 .deliverer = test_regulator_deliverer_logger,
1155 .deliverer_context = &deliverer_logger_context,
1156 .maximum_message_size = MAXIMUM_MESSAGE_LENGTH,
1157 .maximum_messages = 10,
1158 .delivery_thread_priority = 16,
1159 .delivery_thread_stack_size = 0,
1160 .delivery_thread_period = RTEMS_MILLISECONDS_TO_TICKS(1000),
1161 .maximum_to_dequeue_per_period = 2
1162 };
1163
1164 delivered_messages_reset();
1165
1166 sc = rtems_regulator_create(&attributes, ®ulator);
1167 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1168 rtems_test_assert(regulator != NULL);
1169
1170 deliverer_logger_context.regulator = regulator;
1171
1172
1173
1174
1175
1176 tmp_time = time(NULL);
1177 do {
1178 base_time = time(NULL);
1179 } while (tmp_time == base_time);
1180
1181
1182
1183
1184
1185 for (i=1 ; i <= 5 ; i++) {
1186 sc = rtems_regulator_obtain_buffer(regulator, &buffer);
1187 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1188 rtems_test_assert(buffer != NULL);
1189
1190 length = snprintf(message, MAXIMUM_MESSAGE_LENGTH, "message %d", i);
1191 strcpy(buffer, message);
1192
1193 sc = rtems_regulator_send(regulator, buffer, length);
1194 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1195 }
1196
1197
1198
1199
1200 sleep(5);
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213 rtems_test_assert(delivered_message_count == 5);
1214
1215 for (i=0 ; i < 5 ; i++) {
1216 (void) snprintf(message, MAXIMUM_MESSAGE_LENGTH, "message %d", i+1);
1217
1218 match = strncmp(
1219 delivered_messages[i].message,
1220 message,
1221 MAXIMUM_MESSAGE_LENGTH
1222 );
1223 rtems_test_assert(match == 0);
1224 }
1225
1226
1227
1228
1229
1230 rtems_test_assert(delivered_messages[0].processed == delivered_messages[1].processed);
1231 rtems_test_assert(delivered_messages[1].processed != delivered_messages[2].processed);
1232 rtems_test_assert(delivered_messages[2].processed == delivered_messages[3].processed);
1233 rtems_test_assert(delivered_messages[3].processed != delivered_messages[4].processed);
1234
1235
1236
1237
1238
1239 ticks_per_second = rtems_clock_get_ticks_per_second();
1240
1241 base_ticks = delivered_messages[1].processed;
1242 ticks = delivered_messages[2].processed;
1243 rtems_test_assert(ticks_per_second == ticks - base_ticks);
1244
1245 base_ticks = delivered_messages[3].processed;
1246 ticks = delivered_messages[4].processed;
1247 rtems_test_assert(ticks_per_second == ticks - base_ticks);
1248
1249 sc = rtems_regulator_delete(regulator, FIVE_SECONDS);
1250 rtems_test_assert(sc == RTEMS_SUCCESSFUL);
1251
1252 deliverer_logger_context.regulator = NULL;
1253 }
1254
1255
1256 rtems_task test_regulator(rtems_task_argument);
1257
1258
1259
1260
1261
1262 rtems_task test_regulator(rtems_task_argument arg)
1263 {
1264 (void) arg;
1265
1266 TEST_BEGIN();
1267
1268 malloc_trigger_reset();
1269
1270 test_regulator_create_max_dequeue_zero();
1271 test_regulator_create_null_attributes();
1272 test_regulator_create_null_regulator();
1273 test_regulator_create_deliverer_is_null();
1274 test_regulator_create_maximum_messages_is_zero();
1275 test_regulator_create_maximum_message_size_is_zero();
1276 test_regulator_create_maximum_to_dequeue_per_period_is_zero();
1277 test_regulator_create_malloc_regulator_fails();
1278 test_regulator_create_malloc_buffers_fails();
1279 test_regulator_create_delivery_thread_priority_is_zero();
1280 test_regulator_create_partition_create_fails();
1281 test_regulator_create_message_queue_create_fails();
1282 test_regulator_create_task_create_fails();
1283 test_regulator_create_rate_monotonic_create_fails();
1284
1285 test_regulator_delete_null_regulator();
1286 test_regulator_delete_uninitialized_regulator();
1287 test_regulator_delete_OK();
1288
1289 test_regulator_obtain_buffer_null_regulator();
1290 test_regulator_obtain_buffer_uninitialized_regulator();
1291 test_regulator_obtain_buffer_OK();
1292
1293 test_regulator_release_buffer_null_regulator();
1294 test_regulator_release_buffer_uninitialized_regulator();
1295 test_regulator_release_buffer_OK();
1296
1297 test_regulator_send_null_regulator();
1298 test_regulator_send_null_message();
1299 test_regulator_send_length_is_zero();
1300 test_regulator_send_uninitialized_regulator();
1301
1302 test_regulator_send_one_message_OK();
1303 test_regulator_cannot_delete_with_outstanding();
1304
1305 test_regulator_send_multiple_messages_OK();
1306
1307 TEST_END();
1308
1309 rtems_test_exit(0);
1310 }