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
/// A circular buffer, circular queue, ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.
/// This structure lends itself easily to buffering data streams.
///
/// # Examples
/// ```
/// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
///
/// circular_buffer.enqueue(1);
///
/// match circular_buffer.dequeue() {
///     Some(data) => assert_eq!(*data, 1),
///     None => panic!("Data must not be empty")
/// }
/// ```
///
#[derive(Debug)]
pub struct Circular<T> {
    front_index: usize,
    rear_index: usize,
    size: usize,
    internal_vec: Vec<T>,
    capacity: usize,
    push_enabled: bool,
}

impl<T> Circular<T> {
    /// Creates a new instance of circular queue
    ///
    /// # Arguments
    /// * `capacity` - capacity of the queue. note that the real capacity is equal to capacity determined by the argument + 1
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
    /// ```
    pub fn new(capacity: usize) -> Circular<T> {
        Circular {
            front_index: 0,
            rear_index: 0,
            internal_vec: Vec::with_capacity(std::cmp::max(capacity + 1, 1)),
            size: 0,
            push_enabled: true,
            capacity: std::cmp::max(capacity + 1, 1),
        }
    }

    /// Returns number of items in the queue
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
    /// assert_eq!(circular_buffer.size(), 0);
    ///
    /// circular_buffer.enqueue(1);
    /// assert_eq!(circular_buffer.size(), 1);
    /// ```
    pub fn size(&self) -> usize {
        return self.size;
    }

    /// Returns wether queue is empty or not. this implies wether size is equal to 0 or not.
    /// capacity will stay the same.
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
    /// assert_eq!(circular_buffer.empty(), true);
    ///
    /// circular_buffer.enqueue(1);
    /// assert_eq!(circular_buffer.empty(), false);
    /// ```
    pub fn empty(&self) -> bool {
        return self.size == 0;
    }

    /// Returns true if there are no more room for inserting new items.
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
    /// assert_eq!(circular_buffer.full(), false);
    ///
    /// circular_buffer.enqueue(1);
    /// assert_eq!(circular_buffer.full(), true);
    /// ```
    pub fn full(&self) -> bool {
        return (self.rear_index + 1) % self.capacity == self.front_index;
    }

    /// If queue is not full it will insert an element at the end of the queue.
    /// If queue is full, oldest item will be discarded and new item will be inserted at the end of the queue.
    ///
    /// # Arguments
    /// * `element`: item to be inserted in the queue
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
    ///
    /// circular_buffer.enqueue(1);
    /// ```
    pub fn enqueue(&mut self, element: T) {
        // enqueue is only possible on queue with capacity > 1
        // if capacity of queue is not enough then do nothing
        if self.capacity <= 1 {
            return;
        }

        // check if queue is full
        if self.full() {
            self.front_index = (self.front_index + 1) % self.capacity;

            self.size -= 1;
        }

        if self.push_enabled {
            self.internal_vec.push(element);
        } else {
            self.internal_vec[self.rear_index] = element;
        }

        self.push_enabled = !(self.rear_index + 1 == self.capacity) & self.push_enabled;

        self.rear_index = (self.rear_index + 1) % self.capacity;

        self.size += 1;
    }

    /// Returns and discards the item at the front of the queue.
    /// Returns None if there are no items in the queue.
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<usize> = rudac::queue::Circular::new(1);
    ///
    /// circular_buffer.enqueue(1);
    ///
    /// match circular_buffer.dequeue() {
    ///     Some(data) => assert_eq!(*data, 1),
    ///     None => panic!("Data must not be empty")
    /// }
    /// ```
    pub fn dequeue(&mut self) -> Option<&T> {
        if self.empty() {
            return None;
        }

        let element: &T = &self.internal_vec[self.front_index];

        self.front_index = (self.front_index + 1) % self.capacity;

        self.size -= 1;

        return Some(element);
    }

    /// Transforms each element in the queue using the transform function provided
    ///
    /// # Arguments
    /// * `transform`: function that transforms each element of the queue and returns that transformed element
    ///
    /// # Examples
    /// ```
    /// fn all_caps(text: &String) -> String {
    ///     return text.to_uppercase();
    /// }
    ///
    /// let mut circular_buffer: rudac::queue::Circular<String> = rudac::queue::Circular::new(1);
    ///
    /// circular_buffer.enqueue(String::from("element"));
    ///
    /// circular_buffer.map(all_caps);
    ///
    /// match circular_buffer.dequeue() {
    ///     Some(data) => assert_eq!(*data, String::from("ELEMENT")),
    ///     None => panic!("Data must not be None")
    /// }
    /// ```
    pub fn map(&mut self, transform: fn(&T) -> T) {
        for i in 0..self.size() {
            self[i] = transform(&self[i]);
        }
    }

    /// Transforms each element in the queue using the transform closure provided
    ///
    /// # Arguments
    /// * `transform`: closure that transforms each element of the queue and returns that transformed element
    ///
    /// # Examples
    /// ```
    /// let mut circular_buffer: rudac::queue::Circular<String> = rudac::queue::Circular::new(1);
    ///
    /// circular_buffer.enqueue(String::from("element"));
    ///
    /// circular_buffer.map_closure(|text: &String| -> String{text.to_uppercase()});
    ///
    /// match circular_buffer.dequeue() {
    ///     Some(data) => assert_eq!(*data, String::from("ELEMENT")),
    ///     None => panic!("Data must not be None")
    /// }
    /// ```
    pub fn map_closure<F>(&mut self, transform: F)
    where
        F: Fn(&T) -> T,
    {
        for i in 0..self.size() {
            self[i] = transform(&self[i]);
        }
    }

    /// Clears the queue and resets internal flags
    pub fn clear(&mut self) {
        self.internal_vec.clear();

        self.rear_index = 0;
        self.front_index = 0;
        self.size = 0;
        self.push_enabled = true;
    }
}

impl<T> std::ops::Index<usize> for Circular<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        if index >= self.size() {
            panic!("index out of bounds");
        }
        let new_index = (self.front_index + index) % self.capacity;

        return &self.internal_vec[new_index];
    }
}

impl<T> std::ops::IndexMut<usize> for Circular<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        if index >= self.size() {
            panic!("index out of bounds");
        }
        let new_index = (self.front_index + index) % self.capacity;

        return &mut self.internal_vec[new_index];
    }
}

pub struct CircularIterator<'a, T> {
    vec_circular: &'a Circular<T>,
    index: usize,
}

impl<'a, T> std::iter::IntoIterator for &'a Circular<T> {
    type Item = &'a T;
    type IntoIter = CircularIterator<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        CircularIterator {
            vec_circular: &self,
            index: self.front_index,
        }
    }
}

impl<'a, T> std::iter::Iterator for CircularIterator<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<&'a T> {
        if self.index == self.vec_circular.rear_index || self.vec_circular.empty() {
            return None;
        } else {
            let item = &self.vec_circular[self.index];
            self.index = (self.index + 1) % self.vec_circular.capacity;
            return Some(item);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_circular_queue_1() {
        let vc: Circular<String> = Circular::new(0);

        assert_eq!(vc.capacity, 1);
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 0);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn create_circular_queue_2() {
        let vc: Circular<String> = Circular::new(1);

        assert_eq!(vc.capacity, 2);
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 0);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn enqueue_on_capacity_zero() {
        let mut vc: Circular<String> = Circular::new(0);

        vc.enqueue(String::from("element1"));

        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 0);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn enqueue_on_capacity_big() {
        let mut vc: Circular<String> = Circular::new(10);

        vc.enqueue(String::from("element1"));
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 1);
        assert_eq!(vc.size, 1);

        vc.enqueue(String::from("element2"));
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 2);
        assert_eq!(vc.size, 2);

        vc.enqueue(String::from("element3"));
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 3);
        assert_eq!(vc.size, 3);
    }

    #[test]
    fn enqueue_on_full_queue() {
        let mut vc: Circular<String> = Circular::new(3);

        vc.enqueue(String::from("element1"));

        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 1);
        assert_eq!(vc.size, 1);

        vc.enqueue(String::from("element2"));
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 2);
        assert_eq!(vc.size, 2);

        vc.enqueue(String::from("element3"));
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 3);
        assert_eq!(vc.size, 3);

        // now queue is full
        vc.enqueue(String::from("element4"));
        assert_eq!(vc.push_enabled, false);
        assert_eq!(vc.front_index, 1);
        assert_eq!(vc.rear_index, 0);
        assert_eq!(vc.size, 3);

        vc.enqueue(String::from("element5"));
        assert_eq!(vc.push_enabled, false);
        assert_eq!(vc.front_index, 2);
        assert_eq!(vc.rear_index, 1);
        assert_eq!(vc.size, 3);

        vc.enqueue(String::from("element6"));
        assert_eq!(vc.push_enabled, false);
        assert_eq!(vc.front_index, 3);
        assert_eq!(vc.rear_index, 2);
        assert_eq!(vc.size, 3);

        vc.enqueue(String::from("element7"));
        assert_eq!(vc.push_enabled, false);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 3);
        assert_eq!(vc.size, 3);
    }

    #[test]
    fn dequeue_on_queue_capacity_zero() {
        let mut vc: Circular<String> = Circular::new(0);

        assert_eq!(None, vc.dequeue());
    }

    #[test]
    fn dequeue_one_element() {
        let mut vc: Circular<String> = Circular::new(1);

        vc.enqueue(String::from("element1"));

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element1")),
            None => panic!("Element should not be None!"),
        }

        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 1);
        assert_eq!(vc.rear_index, 1);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn dequeue_multiple_elements() {
        let mut vc: Circular<String> = Circular::new(5);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));
        vc.enqueue(String::from("element3"));
        vc.enqueue(String::from("element4"));
        vc.enqueue(String::from("element5"));

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element1")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 1);
        assert_eq!(vc.rear_index, 5);
        assert_eq!(vc.size, 4);

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element2")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 2);
        assert_eq!(vc.rear_index, 5);
        assert_eq!(vc.size, 3);

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element3")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 3);
        assert_eq!(vc.rear_index, 5);
        assert_eq!(vc.size, 2);

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element4")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 4);
        assert_eq!(vc.rear_index, 5);
        assert_eq!(vc.size, 1);

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element5")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 5);
        assert_eq!(vc.rear_index, 5);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn dequeue_when_rear_smaller_than_front_index() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));
        vc.enqueue(String::from("element3"));
        vc.enqueue(String::from("element4"));

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element3")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, false);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 1);
        assert_eq!(vc.size, 1);

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element4")),
            None => panic!("Element should not be None!"),
        }
        assert_eq!(vc.push_enabled, false);
        assert_eq!(vc.front_index, 1);
        assert_eq!(vc.rear_index, 1);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn enqueue_dequeue_of_primitive_data() {
        let mut vc: Circular<i32> = Circular::new(2);

        vc.enqueue(1);
        vc.enqueue(2);

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, 1),
            None => panic!("Element should not be None!"),
        }

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, 2),
            None => panic!("Element should not be None!"),
        }
    }

    #[test]
    fn clear_circular_queue() {
        let mut vc: Circular<String> = Circular::new(5);

        vc.clear();
        assert_eq!(vc.capacity, 6);
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 0);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn clear_queue_with_capacity_zero() {
        let mut vc: Circular<String> = Circular::new(0);

        vc.clear();
        assert_eq!(vc.capacity, 1);
        assert_eq!(vc.push_enabled, true);
        assert_eq!(vc.front_index, 0);
        assert_eq!(vc.rear_index, 0);
        assert_eq!(vc.size, 0);
    }

    #[test]
    fn index_trait() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));

        assert_eq!(*vc[0], String::from("element1"));
        assert_eq!(*vc[1], String::from("element2"));
    }

    #[test]
    fn index_trait_rear_before_front() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));
        vc.enqueue(String::from("element3"));
        vc.enqueue(String::from("element4"));

        assert_eq!(*vc[0], String::from("element3"));
        assert_eq!(*vc[1], String::from("element4"));
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn index_trait_out_of_bounds() {
        let vc: Circular<String> = Circular::new(0);

        &vc[0];
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn index_trait_out_of_bounds_1() {
        let vc: Circular<String> = Circular::new(1);

        &vc[0];
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn index_trait_out_of_bounds_2() {
        let mut vc: Circular<String> = Circular::new(1);

        vc.enqueue(String::from("element1"));

        &vc[1];
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn index_trait_out_of_bounds_3() {
        let mut vc: Circular<String> = Circular::new(3);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));
        vc.enqueue(String::from("element3"));

        &vc[3];
    }

    #[test]
    #[should_panic(expected = "index out of bounds")]
    fn index_trait_out_of_bounds_rear_before_front_index() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));
        vc.enqueue(String::from("element3"));
        vc.enqueue(String::from("element4"));

        &vc[3];
    }

    #[test]
    fn mut_index_trait() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));

        vc[0] = String::from("element3");
        vc[1] = String::from("element4");

        assert_eq!(*vc[0], String::from("element3"));
        assert_eq!(*vc[1], String::from("element4"));
    }

    #[test]
    fn mut_index_trait_dequeue() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));

        vc[0] = String::from("element3");
        vc[1] = String::from("element4");

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element3")),
            None => panic!("Element should not be None!"),
        }

        match vc.dequeue() {
            Some(elem) => assert_eq!(*elem, String::from("element4")),
            None => panic!("Element should not be None!"),
        }
    }

    #[test]
    fn iterator_trait() {
        let mut vc: Circular<String> = Circular::new(2);

        let template = vec!["element1", "element2"];
        let mut index = 0;

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));

        for item in &vc {
            assert_eq!(item, template[index]);
            index += 1;
        }
    }

    #[test]
    fn iterator_trait_on_empty_queue() {
        let vc: Circular<String> = Circular::new(2);

        for _ in &vc {
            panic!("Loop should not get executed");
        }
    }

    fn all_caps(text: &String) -> String {
        return text.to_uppercase();
    }

    #[test]
    fn apply_map() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));

        vc.map(all_caps);

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, String::from("ELEMENT1")),
            None => panic!("Data must not be None"),
        }

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, String::from("ELEMENT2")),
            None => panic!("Data must not be None"),
        }
    }

    fn plus_one(num: &usize) -> usize {
        return *num + 1;
    }

    #[test]
    fn apply_map_primitive_data() {
        let mut vc: Circular<usize> = Circular::new(2);
        vc.enqueue(1);
        vc.enqueue(2);

        vc.map(plus_one);

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, 2),
            None => panic!("Data must not be None"),
        }

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, 3),
            None => panic!("Data must not be None"),
        }
    }

    #[test]
    fn apply_map_closure() {
        let mut vc: Circular<String> = Circular::new(2);

        vc.enqueue(String::from("element1"));
        vc.enqueue(String::from("element2"));

        vc.map_closure(|text: &String| -> String { text.to_uppercase() });

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, String::from("ELEMENT1")),
            None => panic!("Data must not be None"),
        }

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, String::from("ELEMENT2")),
            None => panic!("Data must not be None"),
        }
    }

    #[test]
    fn apply_map_closure_primitive_data() {
        let mut vc: Circular<usize> = Circular::new(2);
        vc.enqueue(1);
        vc.enqueue(2);

        vc.map_closure(|num: &usize| -> usize { num + 1 });

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, 2),
            None => panic!("Data must not be None"),
        }

        match vc.dequeue() {
            Some(data) => assert_eq!(*data, 3),
            None => panic!("Data must not be None"),
        }
    }
}