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
use std::{
    ffi::{c_char, c_int, CStr},
    marker::PhantomData,
    mem::ManuallyDrop,
    ops::{Deref, DerefMut},
    ptr::NonNull,
};

use thiserror::Error;

use crate::{
    api, ffi,
    frame::{AudioFrame, Frame, VideoFrame},
    function::Function,
    node::{AudioNode, Node, VideoNode},
};

mod key;
pub use key::*;

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct MapMut<'m> {
    handle: NonNull<ffi::VSMap>,
    _marker: PhantomData<&'m mut Map>,
}

impl<'m> MapMut<'m> {
    #[must_use]
    pub fn new(handle: NonNull<ffi::VSMap>) -> Self {
        Self {
            handle,
            _marker: PhantomData,
        }
    }

    #[must_use]
    pub(crate) unsafe fn from_ptr(ptr: *mut ffi::VSMap) -> MapMut<'m> {
        MapMut {
            handle: NonNull::new_unchecked(ptr),
            _marker: PhantomData,
        }
    }
}

impl<'m> Deref for MapMut<'m> {
    type Target = Map;

    fn deref(&self) -> &'m Self::Target {
        unsafe { &*(self as *const Self).cast::<Map>() }
    }
}

impl<'m> DerefMut for MapMut<'m> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *(self as *mut Self).cast::<Map>() }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(transparent)]
pub struct MapRef<'m> {
    handle: NonNull<ffi::VSMap>,
    _marker: PhantomData<&'m Map>,
}

impl<'m> MapRef<'m> {
    #[must_use]
    pub fn new(handle: NonNull<ffi::VSMap>) -> Self {
        Self {
            handle,
            _marker: PhantomData,
        }
    }

    #[must_use]
    pub(crate) unsafe fn from_ptr(ptr: *const ffi::VSMap) -> MapRef<'m> {
        MapRef {
            handle: NonNull::new_unchecked(ptr.cast_mut()),
            _marker: PhantomData,
        }
    }
}

impl<'m> Deref for MapRef<'m> {
    type Target = Map;

    fn deref(&self) -> &'m Self::Target {
        unsafe { &*(self as *const Self).cast::<Map>() }
    }
}

#[derive(PartialEq, Eq, Hash, Debug)]
#[repr(transparent)]
pub struct Map {
    handle: NonNull<ffi::VSMap>,
}

impl Map {
    #[must_use]
    pub fn new() -> Self {
        Self {
            // safety: `api.createMap` always returns a valid pointer
            handle: unsafe { NonNull::new_unchecked((api().createMap)()) },
        }
    }

    #[must_use]
    pub(crate) unsafe fn from_ptr(ptr: *mut ffi::VSMap) -> Self {
        Self {
            handle: NonNull::new_unchecked(ptr),
        }
    }

    #[must_use]
    pub fn as_ptr(&self) -> *const ffi::VSMap {
        self.handle.as_ptr()
    }

    #[must_use]
    pub fn as_mut_ptr(&mut self) -> *mut ffi::VSMap {
        self.handle.as_ptr()
    }

    pub fn clear(&mut self) {
        // safety: `self.handle` is a valid pointer
        unsafe { (api().clearMap)(self.as_mut_ptr()) }
    }

    pub fn set_error(&mut self, msg: &CStr) {
        // safety: `self.handle` and `msg` are valid pointers
        unsafe { (api().mapSetError)(self.as_mut_ptr(), msg.as_ptr()) }
    }

    #[must_use]
    pub fn get_error(&self) -> Option<&CStr> {
        let ptr = unsafe { (api().mapGetError)(self.as_ptr()) };
        if ptr.is_null() {
            None
        } else {
            Some(unsafe { CStr::from_ptr(ptr) })
        }
    }

    #[must_use]
    pub fn len(&self) -> i32 {
        // safety: `self.handle` is a valid pointer
        unsafe { (api().mapNumKeys)(self.as_ptr()) }
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[must_use]
    pub fn get_key(&self, index: i32) -> &KeyStr {
        assert!(!(index < 0 || index >= self.len()), "index out of bounds");

        // safety: `self.handle` is a valid pointer
        unsafe { KeyStr::from_ptr((api().mapGetKey)(self.as_ptr(), index)) }
    }

    pub fn delete_key(&mut self, key: &KeyStr) {
        // safety: `self.handle` and `key` are valid pointers
        unsafe { (api().mapDeleteKey)(self.as_mut_ptr(), key.as_ptr()) };
    }

    #[must_use]
    pub fn num_elements(&self, key: &KeyStr) -> Option<i32> {
        // safety: `self.handle` is a valid pointer
        let res = unsafe { (api().mapNumElements)(self.as_ptr(), key.as_ptr()) };
        if res == -1 {
            None
        } else {
            Some(res)
        }
    }

    unsafe fn _get<T>(
        &self,
        func: unsafe extern "system" fn(
            *const ffi::VSMap,
            *const c_char,
            c_int,
            *mut ffi::VSMapPropertyError,
        ) -> T,
        key: &KeyStr,
        index: i32,
    ) -> Result<T, MapPropertyError> {
        let mut error = ffi::VSMapPropertyError::Success;
        handle_get_error(func(self.as_ptr(), key.as_ptr(), index, &mut error), error)
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_int(&self, key: &KeyStr, index: i32) -> Result<i64, MapPropertyError> {
        unsafe { self._get(api().mapGetInt, key, index) }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_float(&self, key: &KeyStr, index: i32) -> Result<f64, MapPropertyError> {
        unsafe { self._get(api().mapGetFloat, key, index) }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    #[allow(clippy::cast_sign_loss)]
    pub fn get_binary(&self, key: &KeyStr, index: i32) -> Result<&[u8], MapPropertyError> {
        use ffi::VSDataTypeHint as dt;

        unsafe {
            if let dt::Unknown | dt::Binary = self._get(api().mapGetDataTypeHint, key, index)? {
                let size = self._get(api().mapGetDataSize, key, index)?;
                let ptr = self._get(api().mapGetData, key, index)?;

                Ok(std::slice::from_raw_parts(ptr.cast(), size as _))
            } else {
                Err(MapPropertyError::InvalidType)
            }
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    #[allow(clippy::cast_sign_loss)]
    pub fn get_utf8(&self, key: &KeyStr, index: i32) -> Result<&str, MapPropertyError> {
        unsafe {
            if let ffi::VSDataTypeHint::Utf8 = self._get(api().mapGetDataTypeHint, key, index)? {
                let size = self._get(api().mapGetDataSize, key, index)?;
                let ptr = self._get(api().mapGetData, key, index)?;

                Ok(std::str::from_utf8_unchecked(std::slice::from_raw_parts(
                    ptr.cast(),
                    size as _,
                )))
            } else {
                Err(MapPropertyError::InvalidType)
            }
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_function(&self, key: &KeyStr, index: i32) -> Result<Function, MapPropertyError> {
        unsafe {
            self._get(api().mapGetFunction, key, index)
                .map(|p| Function::from_ptr(p))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_video_node(&self, key: &KeyStr, index: i32) -> Result<VideoNode, MapPropertyError> {
        unsafe {
            self._get(api().mapGetNode, key, index)
                .map(|p| VideoNode::from_ptr(p))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_audio_node(&self, key: &KeyStr, index: i32) -> Result<AudioNode, MapPropertyError> {
        unsafe {
            self._get(api().mapGetNode, key, index)
                .map(|p| AudioNode::from_ptr(p))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_video_frame(
        &self,
        key: &KeyStr,
        index: i32,
    ) -> Result<VideoFrame, MapPropertyError> {
        unsafe {
            self._get(api().mapGetFrame, key, index)
                .map(|p| VideoFrame::from_ptr(p))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_audio_frame(
        &self,
        key: &KeyStr,
        index: i32,
    ) -> Result<AudioFrame, MapPropertyError> {
        unsafe {
            self._get(api().mapGetFrame, key, index)
                .map(|p| AudioFrame::from_ptr(p))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get(&self, key: &KeyStr, index: i32) -> Result<Value, MapPropertyError> {
        use ffi::VSPropertyType as t;

        unsafe {
            match (api().mapGetType)(self.as_ptr(), key.as_ptr()) {
                t::Unset => Err(MapPropertyError::KeyNotFound),
                t::Int => self.get_int(key, index).map(Value::Int),
                t::Float => self.get_float(key, index).map(Value::Float),
                t::Data => {
                    use ffi::VSDataTypeHint as dt;

                    let size = self._get(api().mapGetDataSize, key, index)?;
                    #[allow(clippy::cast_sign_loss)]
                    match self._get(api().mapGetDataTypeHint, key, index)? {
                        dt::Unknown | dt::Binary => {
                            let ptr = self._get(api().mapGetData, key, index)?;
                            Ok(Value::Data(std::slice::from_raw_parts(
                                ptr.cast(),
                                size as _,
                            )))
                        }
                        dt::Utf8 => {
                            let ptr = self._get(api().mapGetData, key, index)?;
                            Ok(Value::Utf8(std::str::from_utf8_unchecked(
                                std::slice::from_raw_parts(ptr.cast(), size as _),
                            )))
                        }
                    }
                }
                t::Function => self.get_function(key, index).map(Value::Function),
                t::VideoNode => self.get_video_node(key, index).map(Value::VideoNode),
                t::AudioNode => self.get_audio_node(key, index).map(Value::AudioNode),
                t::VideoFrame => self.get_video_frame(key, index).map(Value::VideoFrame),
                t::AudioFrame => self.get_audio_frame(key, index).map(Value::AudioFrame),
            }
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_int_saturated(&self, key: &KeyStr, index: i32) -> Result<i32, MapPropertyError> {
        unsafe { self._get(api().mapGetIntSaturated, key, index) }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_int_array(&self, key: &KeyStr) -> Result<&[i64], MapPropertyError> {
        let mut error = ffi::VSMapPropertyError::Success;
        unsafe {
            let size = self
                .num_elements(key)
                .ok_or(MapPropertyError::KeyNotFound)?;
            let ptr = handle_get_error(
                (api().mapGetIntArray)(self.as_ptr(), key.as_ptr(), &mut error),
                error,
            )?;

            #[allow(clippy::cast_sign_loss)]
            Ok(std::slice::from_raw_parts(ptr, size as _))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_float_saturated(&self, key: &KeyStr, index: i32) -> Result<f32, MapPropertyError> {
        // safety: `self.handle` is a valid pointer
        unsafe { self._get(api().mapGetFloatSaturated, key, index) }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn get_float_array(&self, key: &KeyStr) -> Result<&[f64], MapPropertyError> {
        let mut error = ffi::VSMapPropertyError::Success;
        unsafe {
            let size = self
                .num_elements(key)
                .ok_or(MapPropertyError::KeyNotFound)?;
            let ptr = handle_get_error(
                (api().mapGetFloatArray)(self.as_ptr(), key.as_ptr(), &mut error),
                error,
            )?;

            #[allow(clippy::cast_sign_loss)]
            Ok(std::slice::from_raw_parts(ptr, size as _))
        }
    }

    /// # Panics
    ///
    /// Panics if the key exists or is invalid
    pub fn set_empty(&mut self, key: &KeyStr, type_: ffi::VSPropertyType) {
        // safety: `self.handle` is a valid pointer
        let res = unsafe { (api().mapSetEmpty)(self.as_mut_ptr(), key.as_ptr(), type_) };
        assert!(res != 0);
    }

    unsafe fn _set<T>(
        &mut self,
        func: unsafe extern "system" fn(
            *mut ffi::VSMap,
            *const c_char,
            T,
            ffi::VSMapAppendMode,
        ) -> c_int,
        key: &KeyStr,
        val: T,
        append: ffi::VSMapAppendMode,
    ) -> Result<(), MapPropertyError> {
        handle_set_error(func(self.as_mut_ptr(), key.as_ptr(), val, append))
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError::InvalidType`] if the `key`'s type is not the same with `val`
    ///
    /// # Panics
    ///
    /// Panic if the [`Value::Data`]'s or [`Value::Utf8`]'s len is larger than [`i32::MAX`]
    pub fn set(
        &mut self,
        key: &KeyStr,
        val: Value,
        append: AppendMode,
    ) -> Result<(), MapPropertyError> {
        unsafe {
            match val {
                Value::Int(val) => self._set(api().mapSetInt, key, val, append),
                Value::Float(val) => self._set(api().mapSetFloat, key, val, append),
                Value::Data(val) => handle_set_error((api().mapSetData)(
                    self.as_mut_ptr(),
                    key.as_ptr(),
                    val.as_ptr().cast(),
                    val.len().try_into().unwrap(),
                    ffi::VSDataTypeHint::Binary,
                    append,
                )),
                Value::Utf8(val) => handle_set_error((api().mapSetData)(
                    self.as_mut_ptr(),
                    key.as_ptr(),
                    val.as_ptr().cast(),
                    val.len().try_into().unwrap(),
                    ffi::VSDataTypeHint::Utf8,
                    append,
                )),
                Value::VideoNode(val) => {
                    self._set(api().mapSetNode, key, val.as_ptr().cast_mut(), append)
                }
                Value::AudioNode(val) => {
                    self._set(api().mapSetNode, key, val.as_ptr().cast_mut(), append)
                }
                Value::VideoFrame(val) => self._set(api().mapSetFrame, key, val.as_ptr(), append),
                Value::AudioFrame(val) => self._set(api().mapSetFrame, key, val.as_ptr(), append),
                Value::Function(val) => self._set(api().mapSetFunction, key, val.as_ptr(), append),
            }
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    ///
    /// # Panics
    ///
    /// Panic if the `val.len()` is larger than [`i32::MAX`]
    pub fn set_int_array(&mut self, key: &KeyStr, val: &[i64]) -> Result<(), MapPropertyError> {
        unsafe {
            handle_set_error((api().mapSetIntArray)(
                self.as_mut_ptr(),
                key.as_ptr(),
                val.as_ptr(),
                val.len().try_into().unwrap(),
            ))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    ///
    /// # Panics
    ///
    /// Panic if the `val.len()` is larger than [`i32::MAX`]
    pub fn set_float_array(&mut self, key: &KeyStr, val: &[f64]) -> Result<(), MapPropertyError> {
        unsafe {
            handle_set_error((api().mapSetFloatArray)(
                self.as_mut_ptr(),
                key.as_ptr(),
                val.as_ptr(),
                val.len().try_into().unwrap(),
            ))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn consume_node(
        &mut self,
        key: &KeyStr,
        node: impl Node,
        append: AppendMode,
    ) -> Result<(), MapPropertyError> {
        let mut node = ManuallyDrop::new(node);
        unsafe {
            handle_set_error((api().mapConsumeNode)(
                self.as_mut_ptr(),
                key.as_ptr(),
                node.as_mut_ptr(),
                append,
            ))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn consume_frame(
        &mut self,
        key: &KeyStr,
        frame: impl Frame,
        append: AppendMode,
    ) -> Result<(), MapPropertyError> {
        let frame = ManuallyDrop::new(frame);
        unsafe {
            handle_set_error((api().mapConsumeFrame)(
                self.as_mut_ptr(),
                key.as_ptr(),
                frame.as_ptr(),
                append,
            ))
        }
    }

    /// # Errors
    ///
    /// Return [`MapPropertyError`] if the underlying API does not success
    pub fn consume_function(
        &mut self,
        key: &KeyStr,
        function: Function,
        append: AppendMode,
    ) -> Result<(), MapPropertyError> {
        let function = ManuallyDrop::new(function);
        unsafe {
            handle_set_error((api().mapConsumeFunction)(
                self.as_mut_ptr(),
                key.as_ptr(),
                function.as_ptr(),
                append,
            ))
        }
    }
}

impl Drop for Map {
    fn drop(&mut self) {
        // safety: `self.handle` is a valid pointer
        unsafe { (api().freeMap)(self.as_mut_ptr()) }
    }
}

impl Clone for Map {
    fn clone(&self) -> Self {
        let mut map = Self::new();
        // safety: `self` and `map` are both valid
        unsafe { (api().copyMap)(self.as_ptr(), map.as_mut_ptr()) };
        map
    }
}

impl Default for Map {
    fn default() -> Self {
        Self::new()
    }
}

fn handle_get_error<T>(res: T, error: ffi::VSMapPropertyError) -> Result<T, MapPropertyError> {
    use ffi::VSMapPropertyError as e;
    use MapPropertyError as pe;

    match error {
        e::Success => Ok(res),
        e::Unset => Err(pe::KeyNotFound),
        e::Type => Err(pe::InvalidType),
        e::Index => Err(pe::IndexOutOfBound),
        e::Error => Err(pe::MapError),
    }
}

fn handle_set_error(res: i32) -> Result<(), MapPropertyError> {
    if res == 0 {
        Ok(())
    } else {
        Err(MapPropertyError::InvalidType)
    }
}

#[derive(Clone, Debug)]
pub enum Value<'m> {
    Int(i64),
    Float(f64),
    /// Arbitrary binary data
    ///
    /// # Notes
    ///
    /// Could still be UTF-8 strings because of the API3 compatibility
    Data(&'m [u8]),
    Utf8(&'m str),
    VideoNode(VideoNode),
    AudioNode(AudioNode),
    VideoFrame(VideoFrame),
    AudioFrame(AudioFrame),
    Function(Function),
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Error)]
pub enum MapPropertyError {
    #[error("The requested key was not found in the map")]
    KeyNotFound,
    #[error("The wrong function was used to retrieve the property")]
    InvalidType,
    #[error("The requested index was out of bound")]
    IndexOutOfBound,
    #[error("The map has errors. Use [`Map::get_error`] to retrieve the message")]
    MapError,
}

pub type AppendMode = ffi::VSMapAppendMode;

#[cfg(test)]
mod tests {
    use core::panic;

    use const_str::cstr;
    use testresult::TestResult;

    use crate::set_api_default;

    use super::*;

    #[test]
    fn clear() -> TestResult {
        set_api_default()?;
        let mut map = Map::default();
        let key = crate::key!("what");
        map.set(key, Value::Int(42), AppendMode::Replace)?;

        map.clear();
        match map.get(key, 0) {
            Err(MapPropertyError::KeyNotFound) => Ok(()),
            _ => panic!("Map is not cleared"),
        }
    }

    #[test]
    fn error() -> TestResult {
        set_api_default()?;
        let mut map = Map::default();
        let key = crate::key!("what");
        map.set(key, Value::Float(42.0), AppendMode::Replace)?;

        map.set_error(cstr!("Yes"));
        match map.get_error() {
            Some(msg) => assert_eq!(msg, cstr!("Yes"), "Error message is not match"),
            None => panic!("Error is not set"),
        }
        let res = map.get(key, 0);
        match res {
            Err(MapPropertyError::KeyNotFound) => {}
            _ => panic!("Map is not cleared after setting error"),
        }

        map.set(key, Value::Float(42.0), AppendMode::Replace)?;
        let res = map.get(key, 0);
        match res {
            Err(MapPropertyError::MapError) => {}
            _ => panic!(
                "Map after setting error can only be freed, \
                cleared, or queried for error"
            ),
        }

        Ok(())
    }

    #[test]
    fn len() -> TestResult {
        set_api_default()?;
        let mut map = Map::default();
        let key = crate::key!("what");

        map.set(key, Value::Data(&[42, 43, 44, 45]), AppendMode::Replace)?;
        assert_eq!(1, map.len(), "Number of keys is not correct");

        assert!(!map.is_empty(), "Map is not empty");

        Ok(())
    }

    #[test]
    fn key() -> TestResult {
        set_api_default()?;
        let mut map = Map::default();
        let key = crate::key!("what");

        map.set(key, Value::Float(42.0), AppendMode::Append)?;

        assert_eq!(key, map.get_key(0), "Key is not correct");

        match map.num_elements(key) {
            Some(num) => assert_eq!(1, num),
            None => panic!("Key `{key}` not found "),
        }

        map.delete_key(key);
        assert_eq!(
            0,
            map.len(),
            "Number of keys is not correct after deleting `{key}`"
        );

        Ok(())
    }

    #[test]
    #[allow(clippy::float_cmp)]
    fn get_set() -> TestResult {
        set_api_default()?;
        let mut map = Map::default();
        let key = crate::key!("what");

        let source = i64::from(i32::MAX) + 1;
        map.set(key, Value::Int(source), AppendMode::Replace)?;
        let res = map.get(key, 0)?;
        match res {
            Value::Int(val) => assert_eq!(val, source, "Value of `{key}` is not correct"),
            _ => panic!("Invalid type of `{key}`"),
        }
        let res = map.get_int_saturated(key, 0)?;
        assert_eq!(res, i32::MAX, "Value of `{key}` is not correct");
        map.set(key, Value::Int(source), AppendMode::Append)?;
        assert_eq!(&[source, source], map.get_int_array(key)?);
        map.set_int_array(key, &[1, 2, 3])?;
        assert_eq!(&[1, 2, 3], map.get_int_array(key)?);

        map.set(key, Value::Float(f64::MAX), AppendMode::Replace)?;
        let res = map.get(key, 0)?;
        match res {
            Value::Float(val) => {
                assert_eq!(val, f64::MAX, "Value of `{key}` is not correct");
            }
            _ => panic!("Invalid type of `{key}`"),
        }
        let res = map.get_float_saturated(key, 0)?;
        assert_eq!(res, f32::MAX, "Value of `{key}` is not correct");
        map.set(key, Value::Float(f64::MAX), AppendMode::Append)?;
        assert_eq!(&[f64::MAX, f64::MAX], map.get_float_array(key)?);
        map.set_float_array(key, &[1.0, 2.0, 3.0])?;
        assert_eq!(&[1.0, 2.0, 3.0], map.get_float_array(key)?);

        map.set(key, Value::Data(&[42, 43]), AppendMode::Replace)?;
        let res = map.get(key, 0)?;
        match res {
            Value::Data(val) => {
                assert_eq!(val, &[42, 43], "Value of `{key}` is not correct");
            }
            _ => panic!("Invalid type of `{key}`"),
        }

        map.set(key, Value::Utf8("good"), AppendMode::Replace)?;
        let res = map.get(key, 0)?;
        match res {
            Value::Utf8(val) => {
                assert_eq!(val, "good", "Value of `{key}` is not correct");
            }
            _ => panic!("Invalid type of `{key}`"),
        }

        Ok(())
    }
}